我有一个像这样的字符串
ch:keyword
ch:test
ch:some_text
我需要一个匹配所有字符串的正则表达式,但是,它必须与以下内容不匹配:
ch: (ch: is proceeded by a space, or any number of spaces)
ch: (ch: is proceeded by nothing)
我可以用' ch来推断字符串的长度:'在里面。 任何帮助,将不胜感激;我正在使用PHP的preg_match()
编辑:我试过这个:
preg_match("/^ch:[A-Za-z_0-9]/", $str, $matches)
但是,这只匹配字符串后的1个字符。我尝试在结束的方括号后面放一个*,但这与我不想要的空格相匹配。
答案 0 :(得分:3)
preg_match('/^ch:(\S+)/', $string, $matches);
print_r($matches);
\ S +用于匹配1个或多个非空格字符。这应该适合你。
答案 1 :(得分:2)
试试这个正则表达式:
^ch:\S.*$
答案 2 :(得分:2)
$str = <<<TEXT
ch:keyword
ch:test
ch:
ch:some_text
ch: red
TEXT;
preg_match_all('|ch\:(\S+)|', $str, $matches);
echo '<pre>'; print_r($matches); echo '</pre>';
输出:
Array
(
[0] => Array
(
[0] => ch:keyword
[1] => ch:test
[2] => ch:some_text
)
[1] => Array
(
[0] => keyword
[1] => test
[2] => some_text
)
)
答案 3 :(得分:0)
尝试使用:
preg_match('/(?<! +)ch:[^ ].*/', $str);