I'm not sure whether to post this in beginners or intermediate since it's either really easy or not. I guess I'll find out once it is answered.
How do you match everything but a given string? For example, suppose I want to match strings that don't contain "hello". So "hi", "test" and "moose" would match, but "hello world" and "hello" would not.
This is part of a larger reg exp so I can't just negate the result.
There are several ways to do that; none of them are as simple as they should be, and they all use negative lookahead. Here's one: <blockquote>code:<pre name="code" class="core">str.matches("(?!.*hello).*")</pre></blockquote> Or, if you only want to exclude "hello" as a whole word: <blockquote>code:<pre name="code" class="core">str.matches("(?!.*\\bhello\\b).*")</pre></blockquote>