Search code examples
regexinverse-match

How can I "inverse match" with regex?


I'm processing a file, line-by-line, and I'd like to do an inverse match. For instance, I want to match lines where there is a string of six letters, but only if these six letters are not 'Andrea'. How should I do that?

I'm using RegexBuddy, but still having trouble.


Solution

  • (?!Andrea).{6}
    

    Assuming your regexp engine supports negative lookaheads...

    ...or maybe you'd prefer to use [A-Za-z]{6} in place of .{6}

    Note that lookaheads and lookbehinds are generally not the right way to "inverse" a regular expression match. Regexps aren't really set up for doing negative matching; they leave that to whatever language you are using them with.