tags:

views:

552

answers:

3
+5  Q: 

Regex boolean not

How do I write a .net regex which will match a string that does NOT start with "Seat"

+7  A: 

Writing a regex for a "does not start with" can be a little tricky. It's often easier to write a regex to detect that a string starts with a substring and not the match instead.

For Example:

return !Regex.IsMatch("^Seat.*", input);
JaredPar
Yay for that answer. I like keeping things simple.
PEZ
+10  A: 

What you're looking for is:

^(?!Seat).+

This article has more information about look aheads.

Soviut
Neato. I previously had not seen this construct. Nice!
JaredPar
That is quite useful, I'll have to remember that.
DavGarcia
Mark my answer correct, I'd like the reputation points please ;)
Soviut
A: 

I would suggest not doing it. You should be able to just get every string that doesn't match.

!Regex.IsMatch("^Seat.*", string);
Samuel