Regular Expression IsMatch question

  • Thread starter Thread starter Kate Bieren
  • Start date Start date
K

Kate Bieren

This should be easy but I am brand new to regular expressions so hopefully
someone can help me. I want to do some simple field level validation in my
app and I can do a simple test to see if the value entered in a textbox
matches a value I am expecting -

RegEx.IsMatch(textbox.text,"^LS$"). - So this will return true if my
textbox.text is "LS".

What I want to do though, is to know if the value in my textbox.text does
NOT match "LS".

I know this is lame because obviously I could just say:
if not(regex.ismatch(textbox.text,"^LS$") then....
but I am trying to right some dynamic code to do the validating and I don't
want to have to know if I am testing for true or false.

Does this make sense? Can anyone help me?
 
Kate Bieren said:
This should be easy but I am brand new to regular expressions so hopefully
someone can help me. I want to do some simple field level validation in my
app and I can do a simple test to see if the value entered in a textbox
matches a value I am expecting -

RegEx.IsMatch(textbox.text,"^LS$"). - So this will return true if my
textbox.text is "LS".

What I want to do though, is to know if the value in my textbox.text does
NOT match "LS".

I know this is lame because obviously I could just say:
if not(regex.ismatch(textbox.text,"^LS$") then....
but I am trying to right some dynamic code to do the validating and I
don't
want to have to know if I am testing for true or false.

This really isn't as easy as it should be: The "standard" way to do it would
be using alternations like this:

^(.{0,1}|[^L].|.[^S]|.{3,})$

Read: zero or one character, or anything but an L followed by one character,
or one character followed by anything but an S, or more than 2 characters.

Luckily MS included a special Regex construct that makes this kind of thing
a lot easier: Negative lookahead assertions:

^(?!LS$).*

The (?!...) part tests if the subexpression does not match. If it does, the
rest of the expression is evaluated, and .* always matches.

BTW: Do you know Expresso? (www.ultrapico.com)

Niki
 
Back
Top