Kirzilla
Kirzilla

Reputation: 16596

Get text between "tags" [b]text[/b]

How can I get text between "tags" [b][/b] from this text?

Here is some
[b]
bold text [b]abcd[/b]
[/b]

and here is [b]another bold text[/b]

I neet to get

Please help with regular expression!

Thank you!

Upvotes: 0

Views: 790

Answers (2)

ridgerunner
ridgerunner

Reputation: 34385

Nested structures are easily matched with the (non-REGULAR) recursive patterns available in PHP (i.e. (?R), (?1), (?2) etc.). For example, the following regex matches a (possibly nested) bold BBCode tag:

$re = '%\[B\]((?:(?R)|[^\[]*(?:\[(?!/?B\b)[^\[]*)*)*)\[/B\]%i';

The content between the [B]..[/B] tags is in capture group $1.

Those who say that it can't be done are mistaken.

The new parser I just finished for the FluxBB open source forum software uses a more advanced version of this regex. If you're interested in taking a look at it, see: New FluxBB 2011 Parser Regular Expressions. (But fair warning: this is not for the regex-faint-of-heart!)

p.s. News flash! Perl and .NET can do it too.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359786

Regex is the wrong tool for this task. If you can control the input format, why not use proper XML:

Here is some
<b>
bold text <b>abcd</b>
</b>

and here is <b>another bold text</b>

and then use an XML parser?

Upvotes: 0

Related Questions