zozo
zozo

Reputation: 8602

regexp - Match whole string if it doesn't contain another string

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 :)) ).

Upvotes: 2

Views: 907

Answers (2)

Dávid Horváth
Dávid Horváth

Reputation: 4320

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

!preg_match("#home#", "This is a home bla bla", $part);

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149040

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.

Upvotes: 2

Related Questions