Regex for a match if the target does not contain a phrase

5,292 views
Skip to first unread message

Jake

unread,
Aug 30, 2007, 9:44:59 AM8/30/07
to Regex
I want a regex to return a match for a pattern specifying "does not
contain this phrase".

For example, if I have the phrase "good little dawg", and the target
string "Hey dude", the regex has to return something (with the
find() ), but not with "I have a good little dawg in my house".
I have tried something akin to [^(good little dawg)], but it doesn't
work all the time.

In other words, I need a match if the phrase is *not* in the target
string.
Any idea?
Thanks!

David Portabella

unread,
Sep 19, 2007, 5:32:25 PM9/19/07
to Regex
Someone at experts-exchange told me the solution.
See this example:

([^ ]*) (?!world)([^.]*)

this will match "hello will", but not "hello world".

I didn't find much documentation about it, but you can experiment with
these patterns:

(?=X) X, via zero-width positive lookahead
(?!X) X, via zero-width negative lookahead
(?<=X) X, via zero-width positive lookbehind
(?<!X) X, via zero-width negative lookbehind
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html


DAvid

Reply all
Reply to author
Forward
0 new messages