tags:

views:

41

answers:

1

If I have this string:

"The quick brown fox jumped over the lazy dog. What a nice brown fox that is."

What regex would I use to match the text 'brown fox' but not where the following word is 'that', i.e. (matches in italic):

"The quick brown fox jumped over the lazy dog. What a nice brown fox that is."

+7  A: 

You need a zero-width negative lookahead assertion, i.e.,

brown fox(?! that)
Heinzi
Brilliant, that does the job. You'd get an extra point from me for even knowing what that is called...
Paddy
@Paddy, if you *do* want to match `brown fox` that is followed by a word that starts with `that`, then add a word boundary after `that`: `brown fox(?! that\b)` which matches a string like `brown fox thatcher` but not `brown fox that is`.
Bart Kiers