views:

38

answers:

1

Hi!

Is it possible to write a regexp rule in "one line" that says: neither A nor B.

For example:

String must contain NEITHER "foo" NOR "bar".

Why one line? Because the filtering tool I am using accepts only one line ... I have tried things like (.*foo.*){0}(.*bar.*){0} without enough luck.

+3  A: 
^(?!.*(foo|bar)).*$
Amarghosh
@Amarghosh: What does "?!" mean?
Simon
@Simon: Negative lookahead.
KennyTM
@Amarghosh: Thanks
Simon
@Simon negative look-ahead: `(?!REGEX_1)REGEX_2` will fail if `REGEX_1` matches, but won't consume any characters so that matching of `REGEX_2` will start at the same position. There is positive look-ahead `(?=REGEX_1)REGEX_2` and their look-behind counter parts. Read http://www.regular-expressions.info/lookaround.html
Amarghosh