50

With PCRE, how can you construct an expression that will only match if a string is not found.

If I were using grep (which I'm not) I would want the -v option.

A more concrete example: I want my regexp to match iff string foo is not in the string. So it would match bar would but not foobar.

Joe Beda
  • 2,743
  • 1
  • 19
  • 16

5 Answers5

78

Okay, I have refined my regular expression based on the solution you came up with (which erroneously matches strings that start with 'test').

^((?!foo).)*$

This regular expression will match only strings that do not contain foo. The first lookahead will deny strings beginning with 'foo', and the second will make sure that foo isn't found elsewhere in the string.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
5

Based on Daniel's answer, I think I've got something that works:

^(.(?!test))*$

The key is that you need to make the negative assertion on every character in the string

Joe Beda
  • 2,743
  • 1
  • 19
  • 16
  • I don't believe this will work either; it will match the string 'test'. – Daniel Vandersluis Jun 05 '09 at 18:54
  • Shoot -- you are right. This is good enough for my current scenario as that edge case doesn't hit, but it doesn't completely solve the problem. – Joe Beda Jun 05 '09 at 19:11
  • I deleted my original answer and posted a new one which should solve the problem completely, including the edge case. Basically my original answer denied strings starting with your substring, and yours denied strings containing the substring but not starting with it. Putting them together is the solution. :) – Daniel Vandersluis Jun 05 '09 at 19:15
1

It's indeed almost a duplicate. I guess the regex you're looking for is

(?!foo).*

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
1

Build an expression that matches, and use !match()... (logical negation) That's probably how grep does anyway...

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • I don't have control over the code that evaluates the match directly so this, unfortunately, isn't an option. – Joe Beda Jun 05 '09 at 19:08
-1

You can also do this (in python) by using re.split, and splitting based on your regular expression, thus returning all the parts that don't match the regex, splitting based on what doesn't match a regularexpression

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248