壓縮:m~\{block:([a-z\s_-]+)\}(?:(?!\{/?block:\1\}).)*\{/block:\1\}~xs
實施例中的Perl:
$_ = '{block:string}conte{block:string}nts{/block:string}{/block:string}';
if (m~ # match operator
\{block: ([a-z\s_-]+) \} # opening block structure and capt grp 1
(?: # begin non capt grp
(?! \{/?block: \1 \}) # negative lookahead, don't want backreffed
# open or closed block struct
. # ok, grab this character
)* # end group, do 0 or more times (greedy)
\{/block: \1 \} # closing block structure matching grp 1
~xs) # modifiers: expanded, include newlines
{
print "matched '$&'\n";
}
輸出:
匹配 '{塊:串} NTS {/塊:串}'
來源
2011-09-09 16:30:51
sln
不行的話恐怕:( – Meep3D
是它未能匹配或PHP的正則表達式給你一個錯誤? –
不匹配任何東西:( – Meep3D