Php 重复捕获正则表达式

Php 重复捕获正则表达式,php,regex,pcre,Php,Regex,Pcre,我一直在努力让这个正则表达式发挥作用,我感觉我几乎拥有了它,但我不确定如何获得我想要的结果。我正在使用类似于JSON对象的模拟数据结构,并试图解析参数 该结构类似于组和选项,例如:group\u label:id{option\u 1:id,option\u 2:id…} 到目前为止,我想到的表达方式是 (?:(?:(?<group_name>[a-zA-Z0-9 _]+?):(?<group_id>[0-9]+?){(?:(?:(?<option_name>

我一直在努力让这个正则表达式发挥作用,我感觉我几乎拥有了它,但我不确定如何获得我想要的结果。我正在使用类似于JSON对象的模拟数据结构,并试图解析参数

该结构类似于组和选项,例如:
group\u label:id{option\u 1:id,option\u 2:id…}

到目前为止,我想到的表达方式是

(?:(?:(?<group_name>[a-zA-Z0-9 _]+?):(?<group_id>[0-9]+?){(?:(?:(?<option_name>.+?):(?<option_id>.+?))+?,?)+?},?))+?
这里有一个指向我正在查看的regex测试仪的链接,您可以看到每个组都变成了一个匹配项,但它只捕获每个选项的最后一个,我希望所有选项都有一个匹配项


如果我试图指定字符串的开始和结束,它也会中断,因此我确信这对我来说是一个提示,我正在做的事情是错误的,但我不是正则表达式专家,而且我的运行时间更短。一如既往,我们非常感谢您的任何建议

这里有一个正则表达式,只需查找不同的功能即可提取组和选项(组以
{
结尾,选项以
{
开头,以
}
结尾):

输出:

Array (
    [My Interests] => 379
    [Test Group] => 1234
)
Array (
    [Commercial] => 0
    [Consumer] => 1
    [Wholesale Reseller] => 2
    [Test One] => 1
    [Test 2] => 2 
)
Array (
    [My Interests] => Array (
        [id] => 379
        [options] => Array (
            [Commercial] => 0
            [Consumer] => 1
            [Wholesale Reseller] => 2
        )
    )
    [Test Group] => Array (
        [id] => 1234
        [options] => Array (
            [Test One] => 1
            [Test 2] => 2
         )
    ) 
)
Array (
    [0] => Array (
        [name] => My Interests
        [id] => 379
        [options] => Array (
            [Commercial] => 0
            [Consumer] => 1
            [Wholesale Reseller] => 2
        )
    )
    [1] => Array (
        [name] => Test Group
        [id] => 1234
        [options] => Array (
            [Test One] => 1
            [Test 2] => 2
         )
    ) 
)
如果需要更结构化的输出,可以在获得匹配项后执行以下操作:

$output = array();
for ($i = 0; $i < count($matches['group_name']); $i++) {
    if ($matches['group_name'][$i] != '') {
        // new group
        $this_group = $matches['group_name'][$i];
        $output[$this_group] = array('id' => $matches['group_id'][$i]);
    }
    else {
        // option for this group
        $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i];
    }
}
print_r($output);
或者这可能更有用:

$output = array();
$this_group = -1;
for ($i = 0; $i < count($matches['group_name']); $i++) {
    if ($matches['group_name'][$i] != '') {
        // new group
        $this_group++;
        $output[$this_group] = array('name' => $matches['group_name'][$i], 'id' => $matches['group_id'][$i]);
    }
    else {
        // option for this group
        $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i];
    }
}
print_r($output);

这里有一个正则表达式,它只需查找不同的功能即可提取组和选项(组以
{
结尾,选项以
{
开头,
}
结尾):

输出:

Array (
    [My Interests] => 379
    [Test Group] => 1234
)
Array (
    [Commercial] => 0
    [Consumer] => 1
    [Wholesale Reseller] => 2
    [Test One] => 1
    [Test 2] => 2 
)
Array (
    [My Interests] => Array (
        [id] => 379
        [options] => Array (
            [Commercial] => 0
            [Consumer] => 1
            [Wholesale Reseller] => 2
        )
    )
    [Test Group] => Array (
        [id] => 1234
        [options] => Array (
            [Test One] => 1
            [Test 2] => 2
         )
    ) 
)
Array (
    [0] => Array (
        [name] => My Interests
        [id] => 379
        [options] => Array (
            [Commercial] => 0
            [Consumer] => 1
            [Wholesale Reseller] => 2
        )
    )
    [1] => Array (
        [name] => Test Group
        [id] => 1234
        [options] => Array (
            [Test One] => 1
            [Test 2] => 2
         )
    ) 
)
如果需要更结构化的输出,可以在获得匹配项后执行以下操作:

$output = array();
for ($i = 0; $i < count($matches['group_name']); $i++) {
    if ($matches['group_name'][$i] != '') {
        // new group
        $this_group = $matches['group_name'][$i];
        $output[$this_group] = array('id' => $matches['group_id'][$i]);
    }
    else {
        // option for this group
        $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i];
    }
}
print_r($output);
或者这可能更有用:

$output = array();
$this_group = -1;
for ($i = 0; $i < count($matches['group_name']); $i++) {
    if ($matches['group_name'][$i] != '') {
        // new group
        $this_group++;
        $output[$this_group] = array('name' => $matches['group_name'][$i], 'id' => $matches['group_id'][$i]);
    }
    else {
        // option for this group
        $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i];
    }
}
print_r($output);

可能输入每个
{}
块,然后在
上爆炸:
?我肯定已经考虑过了,我可能会同时这么做。理想情况下,我也想使用正则表达式执行相关的Javascript任务,一石二鸟,良好的学习经验等等。爆炸当然在我的甲板上。我的互联网功能不全,但这里可能有一个粗略的起点输入每个
{}
块,然后在
上爆炸:
?我肯定已经考虑过了,我可能会同时这么做。理想情况下,我也想使用正则表达式执行相关的Javascript任务,一石二鸟,良好的学习经验等等。爆炸当然在我的甲板上。我的互联网功能不全,但这里有一个粗略的起点