将正则表达式的匹配转换为多维数组

时间:2016-06-22 13:01:03

标签: php arrays regex

以下代码正确匹配:

  • letter 'c' followed by digit
  • letter 'c' followed by digit dash digit
  • digit may be followed by an other digit enclosed in square parentheses []

并按预期创建一个数组:

Array 
(
    [0] => Array
    (
        [start] => 4
        [end]=> 8
    )        
    [1] => Array
    (
        [start] => 2
        [end]=> 
    )
    [2] => Array
    (
        [start] => 3
        [end] => 5
    )
    [3] => Array
    (
        [start] => 6[2]
        [end] =>
    )
    [4] => Array
    (
        [start] => 8[4]
        [end] => 10
    )
    [5] => Array
    (
        [start] => 14
        [end] => 21[5]
    )
)

CODE:

 $str = "a2c4-8|a6c2,c3-5,c6[2],c8[4]-10,c14-21[5]";

 $re = "~c(?<start>\d+(?:\[\d+])?)(?:-(?<end>(?&start)?))?~"; 

 $myarray = explode("|",$str);

foreach ($myarray as $my) {
    preg_match_all($re, $my, $matches);

    $res = array_map(function($ms, $me) { 
        return array("start" => $ms, "end" => $me);
    }, $matches["start"], $matches["end"]);
}

echo "<pre>";
    print_r($res);
echo "</pre>";

现在我想在key之后添加另一个a,其值为数字。 例如,鉴于以上$str

$str = "a2c4-8|a6c2,c3-5,c6[2],c8[4]-10,c14-21[5]". 

a值为:26,预期结果数组如下。 请注意,匹配的每个6重复 c

Array 
(
    [0] => Array
    (
        [a] => 2
        [start] => 4
        [end]=> 8
    )        
    [1] => Array
    (
        [a] => 6
        [start] => 2
        [end]=> 
    )
    [2] => Array
    (
        [a] => 6
        [start] => 3
        [end] => 5
    )
    [3] => Array
    (
        [a] => 6
        [start] => 6[2]
        [end] =>
    )
    [4] => Array
    (
        [a] => 6
        [start] => 8[4]
        [end] => 10
    )
    [5] => Array
    (
        [a] => 6
        [start] => 14
        [end] => 21[5]
    )
)

1 个答案:

答案 0 :(得分:1)

你可以这样做:

$str = "a2c4-8|a6c2,c3-5,c6[2],c8[4]-10,c14-21[5]";

$re = '~(?:a(?<a>\d+))?c(?<start>\d+(?:\[\d+])?)(?:-(?<end>\g<start>))?~';

if (preg_match_all($re, $str, $matches, PREG_SET_ORDER) ) {
    $a = ''; // current value for "a" -----,
    $result = array_map(function ($i) use (&$a) {
        if (!empty($i['a'])) $a = $i['a'];
        else $i['a'] = $a;

        if (!isset($i['end'])) $i['end'] = '';

        foreach ($i as $k=>$v) {
            if (is_numeric($k)) unset($i[$k]);
        }

        return $i;

    }, $matches);
}

print_r($result);

但是如果您已经确切知道字符串格式是怎样的,并且如果您不需要对其进行验证,那么可能会有更聪明(更快)的方法来获得相同的结果。

例如,使用explode和格式化的字符串sscanf

$str = "a2c4-8|a6c2,c3-5,c6[2],c8[4]-10,c14-21[5]";

$parts = explode('|', $str);
$result = [];

$keys = ['a', 'start', 'end'];

foreach($parts as $part) {
    foreach(explode(',', $part) as $k=>$v) {
        if ($k)
            list($start, $end) = sscanf($v, 'c%[^-]-%[^,]');
        else
            list($a, $start, $end) = sscanf($v, 'a%dc%[^-]-%[^,]');

        $result[] = array_combine($keys, [$a, $start, $end]);
    }
}

print_r($result);