2

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.

2

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\}'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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