views:

269

answers:

2

I have an ASP.NET website with a regular expression validator text box.

I have changed the expression in the regular expression validation property "validator expression" and after compiling (rebuild) and running, the validation CHANGEs are not reflecting.

The previous validation is working fine but the changed validation is not working.

Please help me!

edit:

First code:

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

Second code:

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

In this part: [a-zA-Z0-9_\-.] you are escaping the hyphen. The proper way to put a hyphen in a regex character class is in first position (or it thinks it is part of a range):

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

Then you removed the backslash from before the @. In Perl the @ would be taken as part of a list name, but in C# I am not sure what effect it would have to not escape it.

The escaping of the periods is also suspect. You might need to double them up: e.g. \\. Instead, what I would do for a period is use a character class: i.e. [.] Inside the character class the period loses its special meaning.

Try this:

@"([-a-zA-Z0-9_.]+)\@((base[.]co[.]uk)|(base[.]com)|(group[.]com)|(arg[.]co[.]uk)|(arggroup[.]com))"
Paul Chernoch
That's odd. With that unescaped hyphen, the original regex shouldn't even have compiled. When I tested it in RegExLib's online tester I got an error saying "[x-y] range in reverse order".
Alan Moore
Escaping the hyphen in a character class to make it a literal work perfectly well with both Perl and .NET. C# does not do variable interpolation like Perl, so there's no need to escape @. [.] matches a literal dot just like \. except that it's one character more to type. Verbatim strings in C# do not require backslashes to be escaped.
Jan Goyvaerts
I once knew of a web site that allowed you to specify which variety of Regular Expression you were testing, and it supported many. I lost track of it. I prefer to use syntax supported widely, as it eases the transition from system to system. What Regex Tester do you like best?
Paul Chernoch
A: 

Assuming your "first code" is a literal regex, you need to escape the hyphen in the character class with a backslash.

Your "second code" is a regex formatted as a C# verbatim string that will match an email address such as [email protected] just fine. There is nothing wrong with this regex.

You'll have to post the code in which you are using this regex if it doesn't work the way you want.

Jan Goyvaerts