views:

129

answers:

1

The item referenced in this question does not seem to work for me. I'm using the Regular Expression validator in .net

I need to pass validation if the input field does NOT look like this

"bagdfsdf -CONST"

When I use "(?>!-CONST)$" and ".*(?>!-CONST)$" the regular expression validator never allows it. If I have -CONST at the end or not

Any ideas?

+4  A: 

(?> … ) is the syntax for an atomic grouping. And the syntax for look-ahead assertion is just (?! … ).


Edit   Try this regular expression instead:

.*$(?<!-CONST)

The .*$ will consume everything and the look-behind assertion will exclude those that end with a -CONST.


Edit    Just for completeness’ sake: If your regular expression language does not allow look-behinds, you can also use this one using a look-ahead assertion:

^(.{0,5}|.*(?!-CONST).{5})$

Or using just alternations:

^(.{0,5}|.*([^-].{5}|-([^C].{4}|C([^O].{3}|O([^N].{2}|N([^S].|S[^T]))))))$
Gumbo
both ValidationExpression=".+(?!-CONST)$" and ValidationExpression="(?!-CONST)$" did not work.
Hogan
@Hogan: You will need a look-behind assertion since there is nothing after the end of the string.
Gumbo
Ah, I think the last one will work Gumbo -- The long list of [^x] almost worked, but not on strings smaller than 5 characters... using the | (ors) to make that work should be good. I will try it on Monday. Thanks! I've not had any luck with the (? style operators, I think they are borked in IE's JavaScript.
Hogan
@Hogan: The first alternation branch `.{0,5}` takes care of strings with up to five characters.
Gumbo
Just tested -- ^(.{0,5}|.*(?!-CONST).{5})$ worked - thanks Gumbo.
Hogan