开发者

Matching nested [quote] in using RegExp

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&开发者_Go百科gt;:

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?


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.


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

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


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


You could use 2 expressions.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜