3

我正在尝试匹配:“{{ loop (something) }}”和第一个 {{ /loop (that something) }} 之间的所有内容

文本示例:

{{ LOOP data }}
  <span> Member key       =  {{.key}}  </span>
  <span> Member value     =  {{.}}     </span>
  <span> Member member    =  {{name}}  </span>
  <span> Iteration number = {{.counter}} </span>
{{ /LOOP data }} 

这是正则表达式不起作用:

/\{\{\s+loop\s+(.+?)\s+\}\}([\w\W]+?)\{\{\s+\/loop\s$1\s+\}\}/i

如您所见,我正在尝试使用“$1”重用第一个匹配的组:我错过了什么?

php代码如下:

$text = '
{{ LOOP data }}
  <span> Member key       =  {{.key}}  </span>
  <span> Member value     =  {{.}}     </span>
  <span> Member member    =  {{name}}  </span>
  <span> Iteration number = {{.counter}} </span>
{{ /LOOP data }}   
';

preg_match('/\{\{\s+loop\s+(.+?)\s+\}\}([\w\W]+?)\{\{\s+\/loop\s\1\s+\}\}/i', $text, $match);
print_r($match);
die;
4

1 回答 1

7

相信你想要\1的,不会$1的。

更多信息:http ://www.regular-expressions.info/brackets.html#usebackrefinregex

于 2013-01-14T13:07:25.137 回答