Sunday, April 17, 2005

SQL Regular Expressions

If you are a Perl, Python or Java developer, you already know the power of regular expressions. The good news is that Oracle 10g supports regular expressions. The syntax is slightly different from what you may have encountered before but it is very easy to adapt if you are already familiar with regular expression syntax.

Here is a very simple example of extracting the domain name from an e-mail address.

select regexp_substr('sshah@whizdog.com', '@([[:alnum:]]+[_|-|.]*[[:alnum:]]*)') from dual


which results '@whizdog.com' being returned.

For more info, please read this excellent article.

2 Comments:

Anonymous Anonymous said...

I hope I am wrong, but does your sample return domains with more than one dot in it?

Does mymail@mycompany.co.uk work?

Cheers!
Reginald

Monday, April 18, 2005 4:45:00 AM  
Blogger whizdog said...

Good point.

This will pattern will handle domains with more than one dot.

select regexp_substr('mymail@mycompany.co.uk', '@([[:alnum:]]+([_|-|.]*[[:alnum:]]*)*)') from dual

Monday, April 18, 2005 8:27:00 AM  

Post a Comment

<< Home