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
- bold text [b]abcd[/b]
- abcd (I will get this using recursion)
- another bold text
Please help with regular expression!
Thank you!
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.
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?
精彩评论