Regex – How to “inverse match” with regex

inverse-matchregex

I'm processing a file line by line. I built a "line model" to match what I want.

Now I'd like to do an inverse match… i.e. 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'll write the program that uses this regex, I don't know yet if in Python or PHP. I'm doing this thing first to learn some regex 🙂 There are different types of line, and I wanted to use regex to select the type I'm interested in. Once I got these lines, I've to apply another filter just to do not match a known value. I need all the others, not that. The (?!not-wanted) is working pretty fine, thank you. 🙂

I'm using RegexBuddy, but I'm in trouble anyway with this thing :\

Best Answer

(?!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.