2013-01-14 95 views
0

我想匹配:之間 一切 「{{環(東西)}}」 和第{{/環路(的東西)}}回用正則表達式匹配

文本例如:

{{ 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; 

回答