2

What I need to do is to get the whole input string, if it doesn't contain another string.

To be more clear (php code, doesn't really matter, the regexp is important):

Let's say the string we want to negate is "home".

 preg_match($unknownReg, "This is a home bla bla", $part);
 echo $part; // I need to echo "";

 preg_match($unknownReg, "This is a car", $part);
 echo $part; // I need to echo "This is a car"

I'm aware of solutions like strpos (for php), but I'd like a reg for it (because not knowing it burns be from inside :)) ).

zozo
  • 8,230
  • 19
  • 79
  • 134

2 Answers2

2

You could try this pattern:

^.*(?<!home.*)$

Or this one:

^(?!.*home).*$

Both patterns will match any sequence of characters as long as it doesn't contain home anywhere in the string. For example:

"This is a home bla bla" // no match
"This is a car"          // match

You can test the second pattern here.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • That looks ok, but I get this: Compilation failed: lookbehind assertion is not fixed length. Second one is ok, so +1 + accept. I'll break down the first later. – zozo Aug 14 '13 at 12:55
  • @zozo I tested it in C#, but it really *does* matter what engine you use. – p.s.w.g Aug 14 '13 at 12:56
  • @zozo For more information on this error, see http://stackoverflow.com/a/3797290/1715579 – p.s.w.g Aug 14 '13 at 12:58
0

Pure negation? To be or !(to be):

!preg_match("#home#", "This is a home bla bla", $part);
Dávid Horváth
  • 4,050
  • 1
  • 20
  • 34