none
Regular Expression for character combinations or strings that should NOT match

    Question

  • 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

    Tuesday, July 19, 2005 10:00 AM

Answers

  • 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
    Wednesday, July 20, 2005 7:24 PM

All replies

  • 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.

    Tuesday, July 19, 2005 12:57 PM
  • 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.
    Wednesday, July 20, 2005 11:26 AM
  • 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
    Wednesday, July 20, 2005 7:24 PM
  • Thanks, that works indeed!

    But I must say I thought things would be simpler than that!
    Friday, July 22, 2005 3:07 PM