我正在尝试使用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
)
当我从输入文本中删除“before 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
)
我已经找了一天,我觉得这会有点愚蠢的问题,但我找不到它......有什么帮助吗?
答案 0 :(得分:0)
这对我有用:
\{(#+)([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*([^\n]*)\s*(\{\1\>firstrow\}\s*(.*?)\s*\{\1\>firstrow\})?\s*(\{\1\>row\}\s*(.*?)\s*\{\1\>row\})?\s*(\{\1\>lastrow\}\s*(.*?)\s*\{\1\>lastrow\})?\s*(.*?)\s*\{\1\}
据我所知(并且很难说)问题是这部分
{\1\>empty\})?\s*(.*?)\s*
具体是(.*?)
它与before rows
不匹配,因为您使用的是\s
标记。由于它不贪婪,.
会在第一场比赛时停止,在这种情况下是新线。
我所做的是将其替换为:
{\1\>empty\})?\s*([^\n]*)\s*
基本上告诉它给我除了换行之外的所有内容,因为我不能在这里使用点运算符。
不确定我的推理是否100%正确,但我的模式应该可以正常工作。