views:

251

answers:

2

I have a regex, for example (ma|(t){1}). It matches ma and t and doesn't match bla.

I want to negate the regex, thus it must match bla and not ma and t, by adding something to this regex. I know I can write bla, the actual regex is however more complex.

+7  A: 

Use negative lookaround: (?!pattern)

Positive lookarounds can be used to assert that a pattern matches. Negative lookarounds is the opposite: it's used to assert that a pattern DOES NOT match. Some flavor supports assertions; some puts limitations on lookbehind, etc.

Links to regular-expressions.info

See also

More examples

These are attempts to come up with regex solutions to toy problems as exercises; they should be educational if you're trying to learn the various ways you can use lookarounds (nesting them, using them to capture, etc):

polygenelubricants
regular-expressions.info is a damn good resource for all things regex.
Freiheit
this simple example only works with ^(?!([m]{2}|(t){1}))
01
nevermind, looked at the wrong docs
M. Jessup
@M. Jessup: it is. http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html, `Ctrl+F` on *"Special constructs (non-capturing)"*.
BalusC
@polygenelubricants: What all have [lookaround](http://www.regular-expressions.info/lookaround.html) support? Does not work with `grep`.
Lazer
+3  A: 

Assuming you only want to disallow strings that match the regex completely (i.e., mmbla is okay, but mm isn't), this is what you want:

^(?!(?:m{2}|t)$).*$

(?!(?:m{2}|t)$) is a negative lookahead; it says "starting from the current position, the next few characters are not mm or t, followed by the end of the string." The start anchor (^) at the beginning ensures that the lookahead is applied at the beginning of the string. If that succeeds, the .* goes ahead and consumes the string.

FYI, if you're using Java's matches() method, you don't really need the the ^ and the final $, but they don't do any harm. The $ inside the lookahead is required, though.

Alan Moore
great explanation,thanks.
01