我有一个正则表达式可以匹配BBcode标签,它很好用,但是有一个小问题。
以下是当前的正则表达式:
\[([^=\[\]]+)[=\x22']*([^ \[\]]*)['\x22]*\](.+)\[/\1\]
这是一些文本,成功匹配的内容以及它建立的组:
[url=http://www.google.com]Go to google![/url] 1:url 2:http://www.google.com 3:前往google!
[img]http://www.somesite.com/someimage.jpg[/img] 1:img 2:NULL 3:http://www.somesite.com/someimage.jpg [quote][quote]first nested quote[/quote][quote]second nested quote[/quote][/quote] 1:quote 2:NULL 3:[quote]first nested quote[/quote][quote]second nested quote[/quote]
所有这些都很好。我可以通过对第三个匹配组运行相同的正则表达式并递归处理所有嵌套的标记来处理嵌套标记。问题在于使用 [quote] 标记的示例。请注意,第三个匹配组是一组两个引用标记,因此我们期望有两个匹配项。但是,我们只得到一个匹配项,如下所示:
[quote]first nested quote[/quote][quote]second nested quote[/quote] 1:quote 2:NULL 3:first nested quote[/quote][quote]second nested quote 啊啊啊!这根本不是我们想要的。有一种相当简单的方法来解决它,我将正则表达式从以下内容修改为:
\[([^=\[\]]+)[=\x22']*([^ \[\]]*)['\x22]*\](.+)\[/\1\]
转化为:
\[([^=\[\]]+)[=\x22']*([^ \[\]]*)['\x22]*\](((?!\[/\1\]).)+)\[/\1\]
通过添加
((?!\[/\1\]).)
,如果第三个匹配组包含关闭BBcode标签,则使整个匹配无效。所以现在这个正则表达式能够工作,我们得到了两个匹配结果:
[quote]first nested quote[/quote][quote]second nested quote[/quote]
[quote]first nested quote[/quote]
1: quote
2: NULL
3: first nested quote[quote]second nested quote[/quote]
1: quote
2: NULL 3: second nested quote
我很高兴它修复了问题,但是现在我们又有了另一个问题。当我们把两个引用标签嵌套在一个更大的引用标签下时,这个新的正则表达式会失败。我们得到了两个匹配结果,而不是一个:
[quote][quote]first nested quote[/quote][quote]second nested quote[/quote][/quote]
[quote][quote]first nested quote[/quote]
1: quote
2: NULL
3: [quote]first nested quote[quote]second nested quote[/quote]
1: quote
2: NULL
3: second nested quote
第一个匹配完全错误,第二个匹配虽然格式正确,但不是我们想要的匹配结果。我们希望得到一个大的匹配,第三个匹配组是两个嵌套的引用标签,就像我们使用第一个表达式时一样。
有什么建议吗?如果我能跨越这个障碍,我应该会有一个相当强大的BBcode表达式。
(?=\[)
就足以仅捕获标签,并且还可以添加一些组来捕获其他标签的名称和内容 - .Net允许对其捕获进行微调。 - Kobi