正则表达式问题(PHP)
[quote=Username here]quoted text here[/quote]
Reply text here
我需要一个正则表达式,将“此处的用户名”、“此处的引用文本”和“此处的回复文本”存储在数组中。
该表达式还需要支持嵌套。埃克斯:
[quote=Username2 here][quote=Username here]quoted text here[/quote]
Reply text here[/quote]
Reply text here
[quote=Username here]quoted text here[/quote]
Reply text here
I need a regular expression that stores the "Username here", "quoted text here" and "Reply text here" in a Array.
This expression needs to support nesting aswell. Eks:
[quote=Username2 here][quote=Username here]quoted text here[/quote]
Reply text here[/quote]
Reply text here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个正则表达式将嵌套引用块(在第 1 组中)与附加的最后回复(在第 2 组中)相匹配:
一个小演示:
产生:
一点解释:
但是,使用 PHP 支持的这些递归正则表达式结构可能会让人头晕……我会选择像约翰·库格曼建议的那样的小解析器。
This regex matches nested quote block (in group 1) with an additional last reply (in group 2):
A little demo:
produces:
A little explanation:
But, using these recursive regex constructs supported by PHP might make ones head spin... I'd opt for a little parser like John Kugelman suggested.
假设您不想返回以某种方式嵌套或与引号匹配的值(这在正则表达式中是不可能的),您可以只拆分不需要的部分:
Assuming you do not want to return the values nested in some way or with quotes matched - which are impossible in a regex - you can just split on the parts you do not need: