我正在寻找这样的正则表达式:
<html>
<body>
@info
<input>.........</input>
@ok_test somthin here
</body>
</html>
我想要得到所有以“@”开头的字符串。 我尝试了 in php,但无法摆脱休息字符串后的空间。 我这样试了试我的正则表达式:
\b@[a-zA-Z0-9]*
但是仍然不能。 有人可以帮我吗? 谢谢。
Try this one:
/(^\@)[^\s]+/gm
It selects:
The \b
word boundary before @
requires a word char (a letter, or digit, or a _
) before it. You must have tried with \B
instead, a non-word boundary. The [a-zA-Z0-9]*
is not matching _
, you should have use \w+
, one or more letters/digits/underscores.
To get multiple matches, you need to use preg_match_all
, not preg_match
with a regex having /g
modifier (that PHP regex does not support).
Use
$input = "@info and more here
Text here: @ok_test somthin here";
preg_match_all('~\B@\w+~', $input, $matches);
print_r($matches[0]);
See PHP demo yielding
Array
(
[0] => @info
[1] => @ok_test
)