我有以下文字,如果只包含preg_match_all
和{{1,我想{
}
和a-zA-Z0-9
内的内容:
}}
some text,{SOMETHING21} {SOMETHI32NG:MORE}some msdf{TEXT:GET:2}sdfssdf sdf sdf
我正在尝试匹配{SOMETHING21}
{SOMETHI32NG:MORE}
{TEXT:GET:2}
标签中可能有多个:
。
我目前拥有的是:
preg_match_all('/\{([a-zA-Z0-9\-]+)(\:([a-zA-Z0-9\-]+))*\}/', $from, $matches, PREG_SET_ORDER);
它符合{SOMETHING21}
和{SOMETHI32NG:MORE}
的预期效果,但对于{TEXT:GET:2}
,它仅匹配TEXT
和2
因此它只匹配标记中的第一个和最后一个单词,并将中间单词留在$matches
数组之外。这是否可能,或者我应该匹配它们然后在:
上爆炸?
- 编辑 -
问题不在于我是否可以获得标签,问题是我是否可以将它们分组而不必再次爆炸结果。即使我当前的正则表达式找到了所有结果,子模式也不会返回$matches
中的所有匹配项。
我希望以下内容能更好地澄清它:
\{ // the match has to start with {
([a-zA-Z0-9\-]+) // after the { the match needs to have alphanum consisting out of 1 or more characters
(
\: // if we have : it should be followed by alphanum consisting out of 1 or more characters
([a-zA-Z0-9\-]+) // <---- !! this is what it is about !! even though this subexpression is between brackets it is not put into $matches if more then one of these is found
)* // there could be none or more of the previous subexpression
\} // the match has to end with }
答案 0 :(得分:2)
您无法获取捕获组的所有匹配值,只能得到最后一个。
所以你必须匹配模式:
preg_match_all('/{([a-z\d-]+(?::[a-z\d-]+)*)}/i', $from, $matches);
然后将$matches[1]
上的每个元素拆分为:
。
答案 1 :(得分:1)
我使用非捕获分组来消除内部组,并且只捕获外部完整的冒号分隔列表。
$from = "some text,{SOMETHING21} {SOMETHI32NG:MORE}some msdf{TEXT:GET:2}sdfssdf sdf sdf";
preg_match_all('/\{((?:[a-zA-Z0-9\-]+)(?:\:(?:[a-zA-Z0-9\-]+))*)\}/', $from, $matches, PREG_SET_ORDER);
print_r($matches);
结果:
Array
(
[0] => Array
(
[0] => {SOMETHING21}
[1] => SOMETHING21
)
[1] => Array
(
[0] => {SOMETHI32NG:MORE}
[1] => SOMETHI32NG:MORE
)
[2] => Array
(
[0] => {TEXT:GET:2}
[1] => TEXT:GET:2
)
)
答案 2 :(得分:0)
也许我不理解这个要求,但是......
preg_match_all('/{[A-Za-z0-9:-]+}/', $from, $matches, PREG_PATTERN_ORDER);
结果:
Array
(
[0] => Array
(
[0] => {SOMETHING21}
[1] => {SOMETHI32NG:MORE}
[2] => {TEXT:GET:2}
)
)