Preg Match All 到单个变量
Preg Match All into single variables
我真的卡在我的代码里了。
我有一个看起来像这样的文本
[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality
Test) [[QVQ]] New Car
我需要 [[QVQ]] 后面的文字。我读到了有关预赛的内容。
preg_match_all('/\[[QVQ\]](.*?)\[[QVQ\]]/s', $input, $matches);
但是在这种情况下如何使用 preg_match 将匹配项放入单个变量中?
喜欢 $match1 , $match2 , $match3 [...]
每个 [
和 ]
都需要转义,而无需转义那些你创建的字符 class 这是允许字符的列表,或者范围 (0-9,将是任何单个数字)。您可以在此处阅读更多内容,http://www.regular-expressions.info/charclass.html。
代码:
preg_match_all('/\[\[QVQ\]\](.*?)\[\[QVQ\]\]/s', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality
Test) [[QVQ]] New Car', $matches);
print_r($matches[1]);
演示:https://eval.in/826134
正则表达式演示:https://regex101.com/r/nHELMW/1/
$matches[1]
将是所有找到的术语的数组。
更新:
因为您实际拥有的是 [[QVQ]]
作为标记,您应该只使用 preg_split
.
$matches = preg_split('/\[\[QVQ\]\]/', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car', -1, PREG_SPLIT_NO_EMPTY);
$matches
将是一个包含所有匹配项的数组。
因为克里斯没有在他的 preg_match_all()
模式中使用环视,所以并不是所有的子串都被捕获。
这将使用 preg_match_all()
捕获所有子字符串(没有任何不需要的 leading/trailing 白色 space 字符):/(?<=\]{2}) ?\K.+?(?=$| \[{2})/
Pattern Demo
var_export(preg_match_all('/(?<=\]{2}) ?\K.+?(?=$| \[{2})/', $input, $out) ? $out[0] : []);
chris 的 preg_split()
方法不与前导和尾随 space 竞争。这是一个 corrected/refined 方法(这是我推荐的正则表达式方法):
var_export(preg_split('/ ?\[{2}QVQ\]{2} ?/', $input, 0, PREG_SPLIT_NO_EMPTY));
这是一个非正则表达式方法:
var_export(array_values(array_filter(array_map('trim', explode('[[QVQ]]', $input)), 'strlen')));
非正则表达式分解:
array_values( // reindex the array
array_filter( // unset any empty elements
array_map('trim', // remove leading/trailing spaces from each element
explode('[[QVQ]]', $input) // split on known delimiter
),
'strlen'
)
)
我真的卡在我的代码里了。
我有一个看起来像这样的文本
[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car
我需要 [[QVQ]] 后面的文字。我读到了有关预赛的内容。
preg_match_all('/\[[QVQ\]](.*?)\[[QVQ\]]/s', $input, $matches);
但是在这种情况下如何使用 preg_match 将匹配项放入单个变量中?
喜欢 $match1 , $match2 , $match3 [...]
每个 [
和 ]
都需要转义,而无需转义那些你创建的字符 class 这是允许字符的列表,或者范围 (0-9,将是任何单个数字)。您可以在此处阅读更多内容,http://www.regular-expressions.info/charclass.html。
代码:
preg_match_all('/\[\[QVQ\]\](.*?)\[\[QVQ\]\]/s', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality
Test) [[QVQ]] New Car', $matches);
print_r($matches[1]);
演示:https://eval.in/826134
正则表达式演示:https://regex101.com/r/nHELMW/1/
$matches[1]
将是所有找到的术语的数组。
更新:
因为您实际拥有的是 [[QVQ]]
作为标记,您应该只使用 preg_split
.
$matches = preg_split('/\[\[QVQ\]\]/', '[[QVQ]]Money [[QVQ]]New Zealand | WATER [ 2nd Test ] [[QVQ]]Who? (Personality Test) [[QVQ]] New Car', -1, PREG_SPLIT_NO_EMPTY);
$matches
将是一个包含所有匹配项的数组。
因为克里斯没有在他的 preg_match_all()
模式中使用环视,所以并不是所有的子串都被捕获。
这将使用 preg_match_all()
捕获所有子字符串(没有任何不需要的 leading/trailing 白色 space 字符):/(?<=\]{2}) ?\K.+?(?=$| \[{2})/
Pattern Demo
var_export(preg_match_all('/(?<=\]{2}) ?\K.+?(?=$| \[{2})/', $input, $out) ? $out[0] : []);
chris 的 preg_split()
方法不与前导和尾随 space 竞争。这是一个 corrected/refined 方法(这是我推荐的正则表达式方法):
var_export(preg_split('/ ?\[{2}QVQ\]{2} ?/', $input, 0, PREG_SPLIT_NO_EMPTY));
这是一个非正则表达式方法:
var_export(array_values(array_filter(array_map('trim', explode('[[QVQ]]', $input)), 'strlen')));
非正则表达式分解:
array_values( // reindex the array
array_filter( // unset any empty elements
array_map('trim', // remove leading/trailing spaces from each element
explode('[[QVQ]]', $input) // split on known delimiter
),
'strlen'
)
)