Shafizadeh
Shafizadeh

Reputation: 10340

How to check string is containing a word and isn't containing another word?

I have a string like this:

$str = "this is a test";

I want to validate $str and return true if string is containing is and it isn't containing test. How can I do that?


Examples:

"this is a test"   // false
"this is a tes"    // true    "is" exists and "test" doesn't exist
"this iss a tes"   // false
"this iss a test"  // false

Here is my pattern \bis\b(?!test). But it seems to just checks existing, I mean it also returns true when test exists. I mean the result of following code us true which shouldn't be (because test exists).

if (preg_match ("/\bis\b(?!test)/","this is a test")) {
    return true;
} else {
    return false;
}

Note: I'm really insist on doing that by regex.

Upvotes: 1

Views: 91

Answers (6)

Jan
Jan

Reputation: 43169

You can do it like this:

^                       # anchor it to the beginning of the line
    (?:(?!\btest\b).)*  # makes sure no test can be matched
    \bis\b              # match is as a word
    (?:(?!\btest\b).)*  # same construct as above
$                       # anchor it to the end of the line

See a demo on regex101.com.

For a PHP code, see the following snippet:

<?php
$string = "this is a test
this is a tes
this iss a tes
this iss a test
this test is";

$regex = '~
            ^                       # anchor it to the beginning of the line
                (?:(?!\btest\b).)*  # makes sure no test can be matched
                \bis\b              # match is as a word
                (?:(?!\btest\b).)*  # same construct as above
            $                       # anchor it to the end of the line
          ~mx';

preg_match_all($regex, $string, $matches);
print_r($matches);
?>

Hint: Note that I have changed the answer after it has been accepted to correct flaws in the original answer).

Upvotes: 1

Vinod Kumawat
Vinod Kumawat

Reputation: 741

Try this it work proper by regular expression

 $str = "this is a test";

     if (preg_match ("/is/",$str) && !preg_match ("/test/",$str)) {
      return false;
    } else {
        return true;
    }

Upvotes: 0

Washington Guedes
Washington Guedes

Reputation: 4365

Try using lookahed, both positive and negative:

^(?=.*\bis\b)(?!.*\btest\b).*

Explaining:

^              # stands for start of the string, both lookahed below will use it as anchor

(?=            # positive lookahed
    .*         # can have any amount of characters till
    \bis\b     # literal text "is" with boundaries
)              # if not succeed will fail the regex

(?!            # negative lookahead
    .*         # can have any amount of characters till
    \btest\b   # literal text "test" with boundaries
)              # if succeed will fail the regex

.*             # if the regex didn't fail till here, match all characters in this line

Upvotes: 2

Pardeep Pathania
Pardeep Pathania

Reputation: 1528

Please try this ^.?\bis\b(?:(?!\btest\b).)$

Upvotes: 1

Ben Rhys-Lewis
Ben Rhys-Lewis

Reputation: 3246

Something like ^(?!.*\btest\b).*\bis\b.*$ so would be:

if (preg_match ("(^(?!.*\btest\b).*\bis\b.*$)","this is a test")) {
    return true;
} else {
    return false;
}

Ok so explanation then, although its obvious, it first checks 'test' doesnt exist with any number of characters before it and then makes sure 'is' does exist.

Upvotes: 1

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

use strpos

$str = "this is a test";
if (strpos($str, 'is') !== false && strpos($str, 'test') === false ) {
   return true;
} else {
  return false;
}

Upvotes: 3

Related Questions