views:

83

answers:

3

I have written the following regular expression. It should match only filenames or folder names that don't end in .aspx.

ValidationExpression=".*[^(\.aspx)]$"

But it does not work. So, kings of RegEx land, please help. I would also like to invalidate extensions such as .Aspx, .aSPx etc. ( I quickly learned that there are some differences between how .net and JScript treat regular expressions).

Can anyone help ?

A: 

The problem you've got is you're using a character class, so you've specified that the last character cannot be '(' or '\' or '.' or 'a' etc.

You need a negative look behind, so something like:

".*(?<!\.aspx)$"

See here for more details. The syntax may be slightly different in ASP.

Al
+2  A: 

[^abc] means "match one character except a, b, or c". You don't want a negated character class, you want negative lookbehind:

ValidationExpression=".*(?<!\.aspx)$"

Use RegexOptions.IgnoreCase as an option in Regex.IsMatch to make it case insensitive.

In JavaScript, where there is no negative lookbehind, you can use a less efficient version with negative lookahead:

ValidationExpression="^(?:(?!\.aspx$).)*$"
Tim Pietzcker
Does this work with java script too ?
No. JavaScript doesn't support lookbehind. You could instead assert that .*\.aspx$ does NOT match your string.
Tim Pietzcker
I am trying to get away just by setting the ValidationExpression.
A: 

As Al pointed out, you're matching a character class instead of ".aspx". The regex you're looking for is:

.*(?<!.aspx$)
Philippe Leybaert
If i use this i get an exception from javascript, when it tries to use the regular expression.