如何通过正则表达式将字符串分解为数组元素?

时间:2021-02-21 22:05:40

I want to convert nested brackets to array with keywords. Here is pattern:

我想将嵌套括号转换为带关键字的数组。这是模式:

preg_match_all('/(?=\{((?:[^{}]++|\{(?0)\})++)\})/', $string, $res);

And data which need to parse:

以及需要解析的数据:

 employee { 
    cashier { salary = 100; } 
    technician { age = 44; } 
 }

Result, that I need:

结果,我需要:

Array
    (
        [employee] => Array (
            [0] => Array
                (
                    [cashier] => Array
                        (
                            [salary] => 100
                        )

                )

            [1] => Array
                (
                    [technician] => Array
                        (
                            [age] => 44
                        )

                )
        )
    )

But cant iterate within nested brackets. Stucked here. Thanks in advance for your help

但是不能在嵌套括号内迭代。被困在这里在此先感谢您的帮助

1 个解决方案

#1


2  

You'll need a recursive approach here.

你需要一个递归方法。

  1. First, analyze the outer structure with { and } on both sides.
  2. 首先,用两侧的{和}分析外部结构。

  3. See, if we can find another nested structure
  4. 如果我们能找到另一个嵌套结构,请参阅

  5. If not, look for key = value pairs and return them
  6. 如果没有,请查找key = value对并​​返回它们

A regex demo for the outer structure can be found on regex101.com, a complete PHP demo would look as follows:

可以在regex101.com上找到外部结构的正则表达式演示,完整的PHP演示如下所示:

<?php

$string = <<<DATA
 employee { 
    cashier { salary = 100; } 
    technician { age = 44; } 
 }
DATA;

// regular expressions    
$outer = '~(?P<key>\w+)\s*(?P<value>\{(?:[^{}]*|(?R))*\})~';

// inner, key = value
$inner = '~(?P<key>\w+)\s*=\s*(?P<value>\w+)~';

function parse($string) {
    global $outer, $inner;
    $result = array();
    // outer
    preg_match_all($outer, $string, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $result[$match["key"]] = parse(
            substr($match["value"], 1, -1)
        );
    }

    // if not found, inner structure
    if (!$matches) {
        preg_match_all($inner, $string, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            $result[$match["key"]] = $match["value"];
        }
        return $result;
    }
    return $result;
}

$result = parse($string);
print_r($result);
?>


This yields:

Array
(
    [employee] => Array
        (
            [cashier] => Array
                (
                    [salary] => 100
                )

            [technician] => Array
                (
                    [age] => 44
                )

        )

)

#1


2  

You'll need a recursive approach here.

你需要一个递归方法。

  1. First, analyze the outer structure with { and } on both sides.
  2. 首先,用两侧的{和}分析外部结构。

  3. See, if we can find another nested structure
  4. 如果我们能找到另一个嵌套结构,请参阅

  5. If not, look for key = value pairs and return them
  6. 如果没有,请查找key = value对并​​返回它们

A regex demo for the outer structure can be found on regex101.com, a complete PHP demo would look as follows:

可以在regex101.com上找到外部结构的正则表达式演示,完整的PHP演示如下所示:

<?php

$string = <<<DATA
 employee { 
    cashier { salary = 100; } 
    technician { age = 44; } 
 }
DATA;

// regular expressions    
$outer = '~(?P<key>\w+)\s*(?P<value>\{(?:[^{}]*|(?R))*\})~';

// inner, key = value
$inner = '~(?P<key>\w+)\s*=\s*(?P<value>\w+)~';

function parse($string) {
    global $outer, $inner;
    $result = array();
    // outer
    preg_match_all($outer, $string, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $result[$match["key"]] = parse(
            substr($match["value"], 1, -1)
        );
    }

    // if not found, inner structure
    if (!$matches) {
        preg_match_all($inner, $string, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            $result[$match["key"]] = $match["value"];
        }
        return $result;
    }
    return $result;
}

$result = parse($string);
print_r($result);
?>


This yields:

Array
(
    [employee] => Array
        (
            [cashier] => Array
                (
                    [salary] => 100
                )

            [technician] => Array
                (
                    [age] => 44
                )

        )

)