.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Regular Expression for character combinations or strings that should NOT match
 

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

Rubbrecht Philippe
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
a280Z28
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.

a280Z28
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.
Rubbrecht Philippe
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
a280Z28
Thanks, that works indeed!

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

You can use google to search for other answers

Custom Search

More Threads

• VS 2010 Beta Officially Released to the Public
• Why System.Object class is not made abstract?
• Wow6432Node
• Application Configuration Settings error "is a type but is used like a variable"
• Strong Name Key or GAC problem/lack of understanding?
• CreateDIBitmap - Black background
• Restrict adding controls in a panel
• Setting pin programmatically for CmsSigner object
• Can i use bodystream and body together in msmq
• Do not throw System.Exception