Regex for Nested BBCode
I'm trying to create a regex that will capture BB Codes, BB Codes with extra parameters ([url=http://]url[/url]
) etc. and work properly with nested BB Codes.
I would then recursively parse the BB Codes starting with the inner most.
This is what I have so far, but it breaks when I try to match nested BB Code.
Pattern:
\[(.*)\b=?([^=].*)?\](.*)\[/\1\]
Visit http://www.gskinner.com/RegExr/ and try the pattern and text below
Try this:
[b]sdfsdf[/b]
[b=e开发者_如何学Pythonxtra]sdfsdf[/b]
[b=extra]left[u]middle[/u]right[/b]
You cannot use regular expressions to make a parser that supports nesting; you'll need to parse the string yourself with a state machine.
Alternatively, reuse existing code.
精彩评论