Question

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?

Was it helpful?

Solution

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

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

OTHER TIPS

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.

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\>/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top