Bender
Bender

Reputation: 1688

Regex boolean not

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

Upvotes: 6

Views: 1706

Answers (3)

Samuel
Samuel

Reputation: 38356

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

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

Upvotes: 0

Soviut
Soviut

Reputation: 91635

What you're looking for is:

^(?!Seat).+

This article has more information about look aheads.

Upvotes: 11

JaredPar
JaredPar

Reputation: 755387

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);

Upvotes: 7

Related Questions