As we all know regular expressions are really powerfull. I'm sure every developer needs them at one time or another and is truly happy that they exist. But today they nearly drove me mad.
Half of the time tools like The Regulator and Expresso do the job and you can almost click your needed expression together, but not today. I needed a regex which would exclude a line if it contained a specific word. So in the following example i want all the lines to match, except the line that contains the word 'ignored':
I want this line.
Give me this line also.
This line must be ignored.
Another line that i want.
After trying various expressions i found that the follwing regex (thanks to this site) did the trick:
^((?!ignored).)*$
In regular expression world '(?!regex)' is called a zero-width negative lookahead which means that the expression wil match if it does not contain regex. The rest of the expression, ^( and .)*$ basically tells the regex engine that the word we look for can be surrounded by other characters e.g. inside a line.