我正在尝试用 PHP 中的正则表达式匹配/替换以下输入文本:
{#var1>var2}
{#>empty}inside empty{#>empty}
before rows
{#>firstrow}inside firstrow{#>firstrow}
{#>row}inside row{#>row}
{#>lastrow}inside lastrow{#>lastrow}
after rows
{#}
其中 var1>var2 是一个数组:
$var1['var2'] = array('key1' => 'value1', 'key2' => 'value2', ...)
我有以下类用正则表达式解析文本(使用 preg_replace_callback):
class parse {
public static function text($text) {
$text = preg_replace_callback('/\{(#+)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)((?:\>[a-zA-Z0-9_\x7f-\xff]*)*)\}\s*(\{\1\>empty\}\s*(.*?)\s*\{\1\>empty\})?\s*(.*?)\s*(\{\1\>firstrow\}\s*(.*?)\s*\{\1\>firstrow\})?\s*(\{\1\>row\}\s*(.*?)\s*\{\1\>row\})?\s*(\{\1\>lastrow\}\s*(.*?)\s*\{\1\>lastrow\})?\s*(.*?)\s*\{\1\}/s', array('parse', 'replace_array'), $text);
return $text;
}
public static function replace_array($matches) {
print_r($matches);
}
}
我得到(不正确的)输出:
Array (
[0] => {#var1>var2>var3} {#>empty}inside empty{#>empty} before rows {#>firstrow}inside firstrow{#>firstrow} {#>row}inside row{#>row} {#>lastrow}inside lastrow{#>lastrow} after rows {#}
[1] => #
[2] => var1
[3] => >var2
[4] => {#>empty}inside empty{#>empty}
[5] => inside empty
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => before rows {#>firstrow}inside firstrow{#>firstrow} {#>row}inside row{#>row} {#>lastrow}inside lastrow{#>lastrow} after rows
)
当我从输入文本中删除“前行”时,我得到了正确的结果:
Array (
[0] => {#var1>var2>var3} {#>empty}inside empty{#>empty} {#>firstrow}inside firstrow{#>firstrow} {#>row}inside row{#>row} {#>lastrow}inside lastrow{#>lastrow} after rows {#}
[1] => #
[2] => var1
[3] => >var2
[4] => {#>empty}inside empty{#>empty}
[5] => inside empty
[6] =>
[7] => {#>firstrow}inside firstrow{#>firstrow}
[8] => inside firstrow
[9] => {#>row}inside row{#>row}
[10] => inside row
[11] => {#>lastrow}inside lastrow{#>lastrow}
[12] => inside lastrow [13] => after rows
)
我已经在寻找一天,我认为这将是一个有点愚蠢的问题,但我找不到它......有什么帮助吗?