Regex/PHP,结尾不匹配

Regex/PHP,结尾不匹配,php,regex,Php,Regex,我试图得到3个匹配的字符串,像这样的一个,由冒号分隔: {{text:1:{"a":"b"}}} 预期结果: match[1] = text match[2] = 1 match[3] = {"a":"b"} 使用以下搜索模式: \{\{(.\*?):(.\*?):(.\*?)\}\} 可悲的是,结果令人失望 match[1] = text match[2] = 1 match[3] = {"a":"b" 如何将正则表达式限制为仅在外部{{}中搜索?尝试删除? 像这样: {{(.*):(

我试图得到3个匹配的字符串,像这样的一个,由冒号分隔:

{{text:1:{"a":"b"}}}
预期结果:

match[1] = text
match[2] = 1
match[3] = {"a":"b"}
使用以下搜索模式:

\{\{(.\*?):(.\*?):(.\*?)\}\}
可悲的是,结果令人失望

match[1] = text
match[2] = 1
match[3] = {"a":"b"

如何将正则表达式限制为仅在外部
{{}
中搜索?

尝试删除

像这样:
{{(.*):(.*):(.*)}


在您的特定情况下,问题是模式中的最后一个
,这阻止了第三个组的贪婪匹配,因此它在第二个
之后的第一个
}
处停止。如果要匹配的字符串始终以双大括号结尾(和开头),您也可以在末尾添加一个
$
(可能在开头添加一个
^
<?php 
$text='{{text:1:{"a":"b"}}}';
preg_match_all("~{{(.*?):(.*?):({.*?})}}~",$text,$match);
print_r($match);

/*output

Array
(
    [0] => Array
        (
            [0] => {{text:1:{"a":"b"}}}
        )

    [1] => Array
        (
            [0] => text
        )

    [2] => Array
        (
            [0] => 1
        )

    [3] => Array
        (
            [0] => {"a":"b"}
        )

)
*/
<?php 
$text='{{text:1:{"a":"b"}}}';
preg_match_all("~{{(.*?):(.*?):({.*?})}}~",$text,$match);
print_r($match);

/*output

Array
(
    [0] => Array
        (
            [0] => {{text:1:{"a":"b"}}}
        )

    [1] => Array
        (
            [0] => text
        )

    [2] => Array
        (
            [0] => 1
        )

    [3] => Array
        (
            [0] => {"a":"b"}
        )

)
*/