This question already has an answer here:
I'm attempting something which I feel should be fairly obvious to me but it's not. I'm trying to match a string which does NOT contain a specific sequence of characters. I've tried using [^ab]
, [^(ab)]
, etc. to match strings containing no 'a's or 'b's, or only 'a's or only 'b's or 'ba' but not match on 'ab'. The examples I gave won't match 'ab' it's true but they also won't match 'a' alone and I need them to. Is there some simple way to do this?
Simplest way is to pull the negation out of the regular expression entirely:
Using a character class such as
[^ab]
will match a single character that is not within the set of characters. (With the^
being the negating part).To match a string which does not contain the multi-character sequence
ab
, you want to use a negative lookahead:And the above expression disected in regex comment mode is:
Using a regex as you described is the simple way (as far as I am aware). If you want a range you could use [^a-f].
Use negative lookahead:
UPDATE: In the comments below, I stated that this approach is slower than the one given in Peter's answer. I've run some tests since then, and found that it's really slightly faster. However, the reason to prefer this technique over the other is not speed, but simplicity.
The other technique, described here as a tempered greedy token, is suitable for more complex problems, like matching delimited text where the delimiters consist of multiple characters (like HTML, as Luke commented below). For the problem described in the question, it's overkill.
For anyone who's interested, I tested with a large chunk of Lorem Ipsum text, counting the number of lines that don't contain the word "quo". These are the regexes I used:
Whether I search for matches in the whole text, or break it up into lines and match them individually, the anchored lookahead consistently outperforms the floating one.
Just search for "ab" in the string then negate the result:
It seems easier and should be faster too.
Yes its called negative lookahead. It goes like this -
(?!regex here)
. Soabc(?!def)
will match abc not followed by def. So it'll match abce, abc, abck, etc.Similarly there is positive lookahead -
(?=regex here)
. Soabc(?=def)
will match abc followed by def.There are also negative and positive lookbehind -
(?<!regex here)
and(?<=regex here)
respectivelyOne point to note is that the negative lookahead is zero-width. That is, it does not count as having taken any space.
So it may look like
a(?=b)c
will match "abc" but it won't. It will match 'a', then the positive lookahead with 'b' but it won't move forward into the string. Then it will try to match the 'c' with 'b' which won't work. Similarly^a(?=b)b$
will match 'ab' and not 'abb' because the lookarounds are zero-width (in most regex implementations).More information on this page