我有如下文所示的嵌套[QUOTE] [/ QUOTE]标签。我想解析整个嵌套字符串usign Perl regex。
[QUOTE username=test1]This is test one. [QUOTE username=test2]This is test2. [/QUOTE] This is test3 [/QUOTE]
我正在使用Perl正则表达式,并使用其结尾[/ QUOTE]首先解析它[QUOTE],但它不解析嵌套标记。
$text =~ s#\[QUOTE\s?(?:username)?(?:=(.*?))?\]([^>]*)?\[\/QUOTE\]#<div class="quoted"><div class="quote-from">$1</div>$2</div>#isg;
只有第一次[QUOTE]解析并返回中间的[QUOTE] [/ QUOTE],它返回到结果以下。但我需要解析所有嵌套标签。
<div class="quoted"><div class="quote-from">test1</div>This is test one. [QUOTE username=test2]This is test2. [/QUOTE] This is test3 </div>
请帮忙解决这个问题。
答案 0 :(得分:1)
你可以通过一些改变来实现它:
[QUOTE]
g
标志不回溯)这样的事情:
1 while $text =~ s#^(.*)\[QUOTE\s*(?:username\s*=\s*([^ \]]+))?\](.*?)\[\/QUOTE\](.*?)#$1<div class="quoted"><div class="quote-from">$2</div>$3</div>$4#s;
结果将是:
<div class="quoted"><div class="quote-from">test1</div>This is test one. <div class="quoted"><div class="quote-from">test2</div>This is test2. </div>This is test3 </div>