我正在开发一个简单的提及系统,在我的PHP脚本中,我需要从较大的文本中提取client:6
,其中将出现一个或多个类似于@[John Doe (#6)](client:6)
的@notice。
Ex。This is my text how do you like it @John and do you have any thoughts @Jane
在php中,字符串看起来像。
This is my text how do you like it @[John Doe (#6)](client:6) and do you have any thoughts @[Jane Doe (#7)](client:7)
我需要得到一个带有array('client:6','client:7')
的阵列
是多种可能的方法之一
@[[^][]+]s*(K[^()]+
请参阅regex101.com上的演示。
就正则表达式而言,这可以归结为
@ # "@" literally
[ # "[" literally
[^][]+ # not "[" nor "]" as many times as possible
]s* # followed by "]" literally + whitespaces, eventually
( # you name it - "(" literally
K # forget all what has been matched that far
[^()]+ # not "(" nor ")"
在PHP
中,这可能是
<?php
$data = "This is my text how do you like it @[John Doe (#6)](client:6) and do you have any thoughts @[Jane Doe (#7)](client:7)";
$regex = "~@[[^][]+]s*(K[^()]+~";
preg_match_all($regex, $data, $matches);
print_r($matches);
?>
并且会产生
Array
(
[0] => Array
(
[0] => client:6
[1] => client:7
)
)
请参阅videone.com.上的演示
w+:d+
应该可以工作。
句中:
这是我的文字,你喜欢它吗@John Doe(#6(和do你有什么想法@Jane Doe(#7(
它应该找到客户端:6和客户端:7。
您可以使用https://regexr.com/例如