【发布时间】:2013-09-12 01:50:35
【问题描述】:
我有这个文本,我正在尝试删除所有内部引号,只保留一个引用级别。引号内的文本包含任何字符,甚至换行符等。 这可以使用正则表达式还是我必须编写一个小解析器?
[quote=foo]I really like the movie. [quote=bar]World
War Z[/quote] It's amazing![/quote]
This is my comment.
[quote]Hello, World[/quote]
This is another comment.
[quote]Bye Bye Baby[/quote]
这里是我想要的文字:
[quote=foo]I really like the movie. It's amazing![/quote]
This is my comment.
[quote]Hello, World[/quote]
This is another comment.
[quote]Bye Bye Baby[/quote]
这是我在 PHP 中使用的正则表达式:
%\[quote\s*(=[a-zA-Z0-9\-_]*)?\](.*)\[/quote\]%si
我也尝试了这个变体,但它与 . 或 , 不匹配,我想不出在引用中还能找到什么:
%\[quote\s*(=[a-zA-Z0-9\-_]*)?\]([\w\s]+)\[/quote\]%i
问题出在这里:
(.*)
【问题讨论】:
-
(.*)有什么问题?不匹配新行? -
@justhalf 问题是它不会停在正确的报价上,而是在最后一个上。
-
@andy 我会检查一下,似乎是一个可行的解决方案,我需要编写一些代码来测试它。