正则表达式问题(PHP)

发布于 2024-08-24 14:26:15 字数 304 浏览 4 评论 0原文

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2024-08-31 14:26:15

这个正则表达式将嵌套引用块(在第 1 组中)与附加的最后回复(在第 2 组中)相匹配:

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

一个小演示:

$text = '[quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text';
preg_match('#(\[quote=[^]]*](?:(?R)|.)*\[/quote])(.*)#is', $text, $match);
print_r($match);

产生:

Array
(
    [0] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text
    [1] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]
    [2] => More text
)

一点解释:

(                  # open group 1
  \[quote=[^]]*]   #   match '[quote= ... ]'
  (?:(?R)|.)*      #   recursively match the entire pattern or any character and repeat it zero or more times
  \[/quote]        #   match '[/quote]'
)                  # open group 1
(                  # open group 2
  .*               #   match zero or more trailing chars after thae last '[/quote]'
)                  # close group 2

但是,使用 PHP 支持的这些递归正则表达式结构可能会让人头晕……我会选择像约翰·库格曼建议的那样的小解析器。

This regex matches nested quote block (in group 1) with an additional last reply (in group 2):

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

A little demo:

$text = '[quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text';
preg_match('#(\[quote=[^]]*](?:(?R)|.)*\[/quote])(.*)#is', $text, $match);
print_r($match);

produces:

Array
(
    [0] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text
    [1] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]
    [2] => More text
)

A little explanation:

(                  # open group 1
  \[quote=[^]]*]   #   match '[quote= ... ]'
  (?:(?R)|.)*      #   recursively match the entire pattern or any character and repeat it zero or more times
  \[/quote]        #   match '[/quote]'
)                  # open group 1
(                  # open group 2
  .*               #   match zero or more trailing chars after thae last '[/quote]'
)                  # close group 2

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.

时光无声 2024-08-31 14:26:15

假设您不想返回以某种方式嵌套或与引号匹配的值(这在正则表达式中是不可能的),您可以只拆分不需要的部分:

preg_split('/(\[quote=|\[quote]|]|\[/quote])/', $yourstring);

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:

preg_split('/(\[quote=|\[quote]|]|\[/quote])/', $yourstring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文