字符串php中的动态占位符

时间:2013-06-20 13:21:39

标签: php regex

一开始,我寻找机会在字符串中发布正则表达式,然后进行所有可能的组合。 因此hallo [A-B]会产生halloA和halloB。 但似乎这是不可能的(Regular expression listing all possibilities

所以现在我正在尝试创造能够handel的东西:

[A-Z]
[a-z]
[0-9]

但我找不到任何有用的东西。

我也发现了这个(PHP dynamic creation of alphabet),但这不是我想要的。

输入:

test[A-B][0-1][x-z]

输出:

testA0x
testA1x
testA0y
testA1y
testB0x
testB1x
testB0y
testB1y

我的课程对我有用:

 <?php

class parser {
    private $groups;
    private $result;

    public function getGroups(){
        return $this->groups;
    }
    public function getResult(){
        return $this->result;
    }

    public function parse($text)
    {
        // Find each character group: [...]
        preg_match_all('/(.*?)(?:\[([^[\]]{1,30})\]|$)/s', $text, $matches, PREG_SET_ORDER);
        $groups = array();
        foreach ($matches as $match) {
            if (!empty($match[1])) {
                // Prefix: foo
                $groups[] = $match[1];
            }
            if (!empty($match[2])) {
                // Group: [a-z0-9]
                // For each range, add the chars to an array. ['a', 'b', ..., 'z', '0', ..., '9']
                $chrs = array();
                preg_match_all('/(.)(?:-(.))?/', $match[2], $ranges, PREG_SET_ORDER);
                foreach ($ranges as $rng)
                {
                    if (empty($rng[2])) {
                        $chrs[] = $rng[1];
                    }
                    else {
                        $chrs = array_merge($chrs, range($rng[1], $rng[2]));
                    }
                }
                $groups[] = $chrs;
            }
        }

        $this->groups = $groups;
        return $groups;
    }

    public function permute($groups, $index = 0)
    {
        $result = array();
        if ($index >= count($groups))
        {
            // Reached the end. Return a single, empty result.
            $result[] = '';
        }
        else if (is_string($groups[$index]))
        {
            // Current group is a simple string. Prepend it to all tail results.
            $prefix = $groups[$index];
            foreach ($this->permute($groups, $index+1) as $s)
            {
                $result[] = $prefix . $s;
            }
        }
        else {
            // Otherwise it is an array of characters. Prepend each to every tail result.
            $chars = $groups[$index];
            foreach ($this->permute($groups, $index+1) as $s)
            {
                foreach ($chars as $ch) {
                    $result[] = $ch . $s;
                }
            }
        }

        $this->result = $result;
        return $result;
    }
}
$text = 'test[A-BXZ][0-1][x-z]foo';
$parser = new parser();

$groups = $parser->parse($text);
print_r($groups);

$permutations = $parser->permute($groups);
print_r($permutations);

?> 

1 个答案:

答案 0 :(得分:0)

<?php
    function parse($text)
    {
        // Find each character group: [...]
        preg_match_all('/(.*?)(?:\[([^[\]]{1,30})\]|$)/s', $text, $matches, PREG_SET_ORDER);
        $groups = [];
        foreach ($matches as $match) {
            if (!empty($match[1])) {
                // Prefix: foo
                $groups []= $match[1];
            }
            if (!empty($match[2])) {
                // Group: [a-z0-9]
                // For each range, add the chars to an array. ['a', 'b', ..., 'z', '0', ..., '9']
                $chrs = [];
                preg_match_all('/(.)(?:-(.))?/', $match[2], $ranges, PREG_SET_ORDER);
                foreach ($ranges as $rng)
                {
                    if (empty($rng[2])) {
                        $chrs []= $rng[1];
                    }
                    else {
                        $chrs = array_merge($chrs, range($rng[1], $rng[2]));
                    }
                }
                $groups []= $chrs;
            }
        }
        return $groups;
    }

    function permute($groups, $index = 0)
    {
        $result = [];
        if ($index >= count($groups))
        {
            // Reached the end. Return a single, empty result.
            $result []= '';
        }
        else if (is_string($groups[$index]))
        {
            // Current group is a simple string. Prepend it to all tail results.
            $prefix = $groups[$index];
            foreach (permute($groups, $index+1) as $s)
            {
                $result []= $prefix . $s;
            }
        }
        else {
            // Otherwise it is an array of characters. Prepend each to every tail result.
            $chars = $groups[$index];
            foreach (permute($groups, $index+1) as $s)
            {
                foreach ($chars as $ch) {
                    $result []= $ch . $s;
                }
            }
        }

        return $result;
    }

    $text = 'test[A-BXZ][0-1][x-z]foo';

    $groups = parse($text);
    print_r($groups);

    $permutations = permute($groups);
    print_r($permutations);
?>

<强>输出:

Array
(
    [0] => test
    [1] => Array
        (
            [0] => A
            [1] => B
            [2] => X
            [3] => Z
        )

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

    [3] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )

    [4] => foo
)
Array
(
    [0] => testA0xfoo
    [1] => testB0xfoo
    [2] => testX0xfoo
    [3] => testZ0xfoo
    [4] => testA1xfoo
    [5] => testB1xfoo
    [6] => testX1xfoo
    [7] => testZ1xfoo
    [8] => testA0yfoo
    [9] => testB0yfoo
    [10] => testX0yfoo
    [11] => testZ0yfoo
    [12] => testA1yfoo
    [13] => testB1yfoo
    [14] => testX1yfoo
    [15] => testZ1yfoo
    [16] => testA0zfoo
    [17] => testB0zfoo
    [18] => testX0zfoo
    [19] => testZ0zfoo
    [20] => testA1zfoo
    [21] => testB1zfoo
    [22] => testX1zfoo
    [23] => testZ1zfoo
)

http://ideone.com/FxrLmC

permute函数也可以写为generator-function