Regular Expressions to NOT match a pattern

Negative lookarounds are usually what you’re after:

Negative Lookbehind

  • (?<!status )code

    matches code not preceded by status

  • (?<!status )(?<!post)code

    matches code not preceded by status or by post

Negative Lookahead

  • code(?!smith)

    matches code not followed by smith

  • code(?!smith)(?!d)

    matches code not followed by smith or by d

Negated Character Classes

But to not-match just a single character you can do it with character a class:

  • code[^d]

    matches code but not coded

  • code=[^0-9]

    matches code=X where X is not a digit

Learn more…

Leave a Reply

Your email address will not be published. Required fields are marked *