Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
What is it intended to do? –  Mechanical snail Aug 3 '11 at 11:34
    
To match the email. –  burgeris 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 –  burgeris Aug 3 '11 at 11:39

1 Answer 1

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\}'
share|improve this answer
    
Thanks! Just what I was looking for. –  burgeris Aug 4 '11 at 11:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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