Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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
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 – Mechanical snail Aug 3 '11 at 11:38
The actual specification is – Mechanical snail Aug 3 '11 at 11:39
Yeah, I know that, but I'm testing it on a certain email - – burgeris Aug 3 '11 at 11:39
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.


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


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.