Regular Expressions to NOT match a pattern

Negative lookarounds are usually what you're after:

Negative Lookbehind

  • [code](?<!status )code[/code] matches code not preceded by status
  • [code](?<!status )(?<!post)code[/code] matches code not preceded by status or by post

Negative Lookahead

  • [code]code(?!smith)[/code] matches code not followed by smith
  • [code]code(?!smith)(?!d)[/code] 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]code[^d][/code] matches code but not coded
  • [code]code=[^0-9][/code] matches code=X where X is not a digit

Learn more...

Regular Expressions to NOT match a patte…

by Chris F Carroll read it in 1 min
0