How to negate the whole regex?

Use negative lookaround: (?!pattern)

Positive lookarounds can be used to assert that a pattern matches. Negative lookarounds is the opposite: it’s used to assert that a pattern DOES NOT match. Some flavor supports assertions; some puts limitations on lookbehind, etc.

Links to regular-expressions.info

  • Lookahead and Lookbehind Zero-Width Assertions
  • Flavor comparison

See also

  • How do I convert CamelCase into human-readable names in Java?
  • Regex for all strings not containing a string?
  • A regex to match a substring that isn’t followed by a certain other substring.

More examples

These are attempts to come up with regex solutions to toy problems as exercises; they should be educational if you’re trying to learn the various ways you can use lookarounds (nesting them, using them to capture, etc):

  • codingBat plusOut using regex
  • codingBat repeatEnd using regex
  • codingbat wordEnds using regex

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)