Regex Inverse Matching

Came across a situation today were I need to inverse match on a string. In other words I want to match anything that is not xyz.

Here is how to do it using negative look-ahead:

^((?!xyz).+)$

Alternatively you can use negative look-behind:

^(.+(?<!xyz))$

2 Comments

  1. shivang
    Posted July 24, 2009 at 7:24 AM | Permalink

    Hi Marco,

    Thanks for this very easy and off course effective regex.

    Regards,
    shivang

  2. Paul
    Posted July 24, 2009 at 10:38 AM | Permalink

    Fantastic, thanks for this!


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*