views:

163

answers:

3

I have the following regular expression in a asp.net email test box:

([a-zA-Z0-9_\-\.]+)\@((base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com))

This regular expression only works for

[email protected]
[email protected]
[email protected]

It's not working for:

[email protected]
[email protected]

Please help me

+6  A: 

Although I didn't get to the exact problem, You missed/overlooked an important point here. :-)

'.' is a wild card in regex, it matches anything.

to match '.' use

\.

lets try this

([a-zA-Z0-9_\-.]+)@(base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com)
Ratnesh Maurya
Can repost the correct expression from above ; i am confused now
@Jaison edited my post lets try that
Ratnesh Maurya
@Rats: no need to escape a dot (.) in a character range ([]). A dot in a character ranges only matches a dot.
Andrew Moore
@Andrew thanks!! i was not aware of that
Ratnesh Maurya
I am with Andrew - a dot has no special meaning inside a character group.
Daniel Brückner
+10  A: 

This:

[a-zA-Z0-9_-.]

contains a bogus range, underscore to dot. Did you mean "underscore, dash or dot" like this:

[a-zA-Z0-9_\-.]

? Once I fix that, it works for me.

You should also use \. rather than . in the addresses - dot matches any character, so you'd match baseXcom with your pattern. (Note that you don't need a backslash for dots within []s.)

The full corrected expression:

@"([a-zA-Z0-9_\-.]+)@((base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com))"

(note that I'm using @"" to avoid having to backslash my backslashes.)

RichieHindle
Its not working still
according to http://www.regexplanet.com/simple/index.jsp this does match all your examples. Maybe your fault is somewhere else?
Ralph Rickenbach
A: 

Using the regular expression tester here will probably help you. However, as far as I can see, there's an error in the regular expression that should prevent it from working with any of the examples: the group should be:

[-a-zA-Z0-9_.]

With that fix, it works with all of your examples. Having the - between the _ and the . tries to construct a range, but the ASCII values are in the wrong order.

Also the dot used in the end part should be escaped as base\.com rather than base.com

Al