preg_match 匹配所有以以下开头和结尾的单词

preg_match match a word all that starts and ends with

我想匹配所有在 Condition is\s 之后开始并以 .,

结束的字符
$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 )

上面的代码可以工作,但如果我输入 space,它将无法工作。

test-name

我试过模式$pattern = "/Condition is\s([a-zA-Z_- ]+)/";

with space 它给出了一个错误

我会使用这种模式:

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

示例脚本:

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