tags:

views:

150

answers:

1

Short version:

How can I get a regex that matches [email protected] but not [email protected] using CAtlRegExp ?


Long version:

I'm using CAtlRegExp http://msdn.microsoft.com/en-us/library/k3zs4axe(VS.80).aspx to try to match email addresses. I want to use the regex

^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$

extracted from here. But the syntax that CAtlRegExp accepts is different than the one used there. This regex returns the error REPARSE_ERROR_BRACKET_EXPECTED, you can check for yourself using this app: http://www.codeproject.com/KB/string/mfcregex.aspx

Using said app, I created this regex:

^[a-zA-Z0-9\._%\+\-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]$

But the problem is this matches [email protected] as valid, I need it to match 4 characters maximum for the op-level domain.

So, how can I get a regex that matches [email protected] but not [email protected] ?

+1  A: 

Try: ^[a-zA-Z0-9\._%\+\-]+@([a-zA-Z0-9-]+\.)+\c\c\c?\c?$

This expression replaces the [A-Z]{2,4} sequence which CAtlRegExp doesn't support with \c\c\c?\c?

\c serves as an abbreviation of [a-zA-Z]. The question marks after the 3rd and 4th \c's indicate they can match either zero or one characters. As a result, this portion of the expression matches 2, 3 or 4 characters, but neither more nor less.

Oren Trutner