正则表达式BBCode-完善嵌套报价 [英] Regex & BBCode - Perfecting Nested Quote

查看:130
本文介绍了正则表达式BBCode-完善嵌套报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站编写一些BBcode.

I'm working on some BBcode for my website.

我设法使大多数代码都能正常工作,但是[QUOTE]标签给我带来了一些麻烦.

I've managed to get most of the codes working perfectly, however the [QUOTE] tag is giving me some grief.

当我得到这样的信息时:

When I get something like this:

[QUOTE=1]
[QUOTE=2]
This is a quote from someone else
[/QUOTE]
This is someone else quoting someone else
[/QUOTE]

它将返回:

> 1 said:  [QUOTE=2]This is a quote from
> someone else

This is someone else quoting someone else[/QUOTE]

所以发生的是嵌套引号中的[/quote]正在关闭引号块.

So what is happening is the [/quote] from the nested quote is closing the quote block.

我正在使用的正则表达式是:

The Regex I am using is:

"[quote=(.*?)\](.*?)\[/quote\]'is"

如何使嵌套的引号正确显示?

How can I make it so nested Quotes will appear properly?

谢谢.

推荐答案

您可以构造递归正则表达式(根据libpcre-3.0的更改日志提供):

You could construct recursive regular expression (available since libpcre-3.0 according to their changelog):

\[quote=(.*?)\](((?R)|.)*?)\[\/quote\]

但是如果您遵循@codeka的建议,那就更好了.

But it would be better if you follow @codeka advice.

更新: (?R)在这里意味着«在(?R)出现的位置插入整个正则表达式».因此,a(?R)?b等价于(如果您忘记了捕获组的话)等价于a(a(?-1)?b)?b,等价于a(a(a(?-1)?b)?b)?b等.可以使用(?N)(?+N)(?-N)(?&a)代替(?R),这意味着用第N个捕获组替代",用第N个下一个捕获组替代",用N替代"第一个上一个捕获组",然后替换为名为"a"的捕获组".

Update: (?R) here means «insert the whole regular expression in place where (?R) occurs». So a(?R)?b is equivalent (if you forget about capturing groups) to a(a(?-1)?b)?b which is equivalent to a(a(a(?-1)?b)?b)?b and so on. Instead of (?R) you can use (?N), (?+N), (?-N) and (?&a) which means «substitute with N'th capturing group», «substitute with N'th next capturing group», «substitute with N'th previous capturing group» and «substitute with capturing group named «a»».

这篇关于正则表达式BBCode-完善嵌套报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆