I'm looking for regex pattern like this:
我正在寻找像这样的正则表达式模式:
<html>
<body>
@info
<input>.........</input>
@ok_test somthin here
</body>
</html>
I want to get all strings which begin with '@'. I tried explode
in php but can't get rid of the rest string after space. I tried my regex like this :
我想得到所有以'@'开头的字符串。我尝试在php中爆炸,但无法摆脱空间后的其余字符串。我尝试了这样的正则表达式:
\b@[a-zA-Z0-9]*
But still can't. Could someone help me? Thanks.
但还是不行。有人能帮帮我吗?谢谢。
2 个解决方案
#1
0
Try this one:
试试这个:
/(^\@)[^\s]+/gm
It selects:
- every line starting with @
- all the following characters up to whitespace [^\s]+
每行以@开头
所有以下字符直到空格[^ \ s] +
#2
0
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.
@之前的\ b字边界需要一个字符char(字母,数字或_)。您必须尝试使用\ B而不是非字边界。 [a-zA-Z0-9] *与_不匹配,你应该使用\ w +,一个或多个字母/数字/下划线。
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).
要获得多个匹配,您需要使用preg_match_all,而不是preg_match和带有/ g修饰符的正则表达式(PHP正则表达式不支持)。
Use
$input = "@info and more here\nText here: @ok_test somthin here";
preg_match_all('~\B@\w+~', $input, $matches);
print_r($matches[0]);
See PHP demo yielding
请参阅PHP演示产生
Array
(
[0] => @info
[1] => @ok_test
)
#1
0
Try this one:
试试这个:
/(^\@)[^\s]+/gm
It selects:
- every line starting with @
- all the following characters up to whitespace [^\s]+
每行以@开头
所有以下字符直到空格[^ \ s] +
#2
0
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.
@之前的\ b字边界需要一个字符char(字母,数字或_)。您必须尝试使用\ B而不是非字边界。 [a-zA-Z0-9] *与_不匹配,你应该使用\ w +,一个或多个字母/数字/下划线。
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).
要获得多个匹配,您需要使用preg_match_all,而不是preg_match和带有/ g修饰符的正则表达式(PHP正则表达式不支持)。
Use
$input = "@info and more here\nText here: @ok_test somthin here";
preg_match_all('~\B@\w+~', $input, $matches);
print_r($matches[0]);
See PHP demo yielding
请参阅PHP演示产生
Array
(
[0] => @info
[1] => @ok_test
)