Regular Expression for character combinations or strings that should NOT match

Is it possible to specify a regular expression pattern for strings that should NOT match something.

For example, I get a string that specifies a number of codes like: VV, VVP, PVT, XXP, G, BB.

I would like to select all codes from the string that differ from VV or XX (so XXP is a valid code!).

Is there an easy way to do this using regular expressions?!

Best regards,
Philippe

[397 byte] By [RubbrechtPhilippe] at [2007-12-16]
# 1
This should be what you are after:

http://msdn2.microsoft.com/library/bs2twtah(en-us,vs.80).aspx

(?! )

Zero-width negative lookahead assertion. Continues match only if the subexpression does not match at this position on the right. For example, \b(?!un)\w+\b matches words that do not begin with un.

Combined with an alternation construct:

http://msdn2.microsoft.com/library/36xybswe(en-us,vs.80).aspx

|

Matches any one of the terms separated by the | (vertical bar) character; for example, cat|dog|tiger. The leftmost successful match wins.

280Z28 at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Thanks for the information, but using \b(?!XX) would also rule out code XXP from the example in my first post, which is a valid code.
RubbrechtPhilippe at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
What characters are allowed? Only uppercase A-Z?

\b(?!XX[^A-Z])[A-Z]+\b

would match all codes that can be formed by the letters A-Z except for XX.

you can replace "+" with "{1,3}" if the codes can only be 1-3 letters:

\b(?!XX[^A-Z])[A-Z]{1,3}\b

to do the same, except this time match all 1-3 letter codes except VY and XX:

\b(?!(?:XX|VY)\b)[A-Z]{1,3}\b

280Z28 at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
Thanks, that works indeed!

But I must say I thought things would be simpler than that!

RubbrechtPhilippe at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified