B L Praveen
B L Praveen

Reputation: 2010

preg_match match a word all that starts and ends with

I want to match all the characters that starts after Condition is\s and and ends with either . or a ,

$pattern = "/Condition is\s([a-zA-Z_-]+)/";
$string = "Condition is test_name. some test";
$string1 = "Condition is test-hello-name, some test";
preg_match($pattern,$string,$match);
Output:
Array ( [0] => Condition is test_name [1] => test_name )
preg_match($pattern,$string,$match);
Output:
Array ( [0] => Condition is test-hello-name [1] => test-hello-name )

Above code is working but if I enter space it will not work.

test-name

I tried pattern $pattern = "/Condition is\s([a-zA-Z_- ]+)/";

with space it gives an error

Upvotes: 0

Views: 469

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522824

I would use this pattern:

\bCondition is\s+([^.,]+)

Sample script:

$string = "Condition is test_name. some test";
preg_match_all("/\bCondition is\s+([^.,]+)/", $string, $matches);
echo $matches[1][0];  // test_name

Upvotes: 1

Related Questions