I can't get this simple regular expression to work for matching emails:

'\w*(?:\.\w*)*@\w*(?:\.\w*)*\w\{2,5\}'

It should be working as I have tested it with regex pal and it works just fine. I think there's a problem with optional character class but I'm not sure.

link|improve this question
1  
What is it intended to do? – Mechanical snail Aug 3 '11 at 11:34
To match the email. – bah Aug 3 '11 at 11:35
Then it's wrong. A lot more than \word characters are permitted in an e-mail address. See en.wikipedia.org/wiki/Email_address#Syntax. – Mechanical snail Aug 3 '11 at 11:38
The actual specification is tools.ietf.org/html/rfc5322. – Mechanical snail Aug 3 '11 at 11:39
Yeah, I know that, but I'm testing it on a certain email - email.something@blah.mailas.com – bah Aug 3 '11 at 11:39
show 3 more comments
feedback

1 Answer

up vote 1 down vote accepted

You should use grep with perl regular expression (-P option) which supports lookahead assertions like (?: ). Also curly braces shouldn't be escaped.

Try:

grep -P '\w*(?:\.\w*)*@\w*(?:\.\w*)*\w{2,5}'

Since perl expressions are experimental feature in GNU grep you may want to change (?: ) to ( ) and user extended expressions (-E):

grep -E '\w*(\.\w*)*@\w*(\.\w*)*\w{2,5}'

Some of the extended expression implementations do not support curly braces { and }. For portability you can use basic regular expressions.

To use basic regular expressions escape ( and ) and leave also { and } escaped.

grep '\w*\(\.\w*\)*@\w*\(\.\w*\)*\w\{2,5\}'
link|improve this answer
Thanks! Just what I was looking for. – bah Aug 4 '11 at 11:00
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.