Matching nested [quote] in using RegExp

核能气质少年 提交于 2019-12-25 04:51:57

问题


I'm trying to get regexp to match some nested tags. (Yes I know I should use a parser, but my input will be correct).

Example:

Text.
More text.
[quote]
First quote
[quote]
Nested second quote.
[/quote]
[/quote]

Let's say I want the regexp to simply change the tags to <blockquote>:

Text.
More text.
<blockquote>
First quote
<blockquote>
Nested second quote.
</blockquote>
</blockquote>

How would I do this, matching both opening and closing tags at the same time?


回答1:


You can't match (arbitrarily) nested stuff with regular expressions.

But you can replace every instance of [quote] with <blockquote> and [/quote] with </blockquote>.




回答2:


If you don’t mind correctness, then you could use a simple string replacement and replace each tag separately. Here’s some example using PHP’s str_replace to replace the opening and closing tags:

$str = str_replace('[quote]', '<blockquote>', $str);
$str = str_replace('[/quote]', '</blockquote>', $str);

Or with the help of a regular expression (PHP again):

$str = preg_replace('~\[(/?)quote]~', '<$1blockquote>', $str);

Here the matches of \[(/?)quote] are replaced with <$1blockquote> where $1 is replaced with the match of the first group of the pattern ((/?), either / or empty).

But you should really use a parser that keeps track of the opening and closing tags. Otherwise you can have an opening or closing tag that doesn’t have a counterpart or (if you’re using further tags) that is not nested properly.




回答3:


It's a lousy idea, but you're apparently trying to match something like: \[\(/?\)quote\] and replace it with: <\1blockquote>




回答4:


You could use 2 expressions.

s/\[quote\]/\<blockquote\>/
s/\[\/quote\]/\<\/blockquote\>/


来源:https://stackoverflow.com/questions/2373890/matching-nested-quote-in-using-regexp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!