据我了解(从递归作为子模式中的条件),这是一个非常基本的示例。
$str = 'ds1aadfg346fgf gd4th9u6eth0';
preg_match_all('~(?(R).(?(?=[^\d])(?R))|\d(?R)?)~'
/*
(? # [begin outer cond.subpat.]
(R) # if this is a recursion ------> IF
. # match the first char
(? # [begin inner cond.subpat.]
(?=[^\d]) # if the next char is not a digit
(?R) # reenter recursion
) # [end inner cond.subpat.]
| # otherwise -----> ELSE
\d(?R)? # match a digit and enter recursion (note the ?)
) # [end outer cond.subpat.]
*/
,$str,$m);
print_r($m[0]);
和输出:
Array
(
[0] => 1aadfg
[1] => 34
[2] => 6fgf gd
[3] => 4th
[4] => 9u
[5] => 6eth
[6] => 0
)
我知道这是一个愚蠢的例子,但我希望它是有道理的。