开发者

Regex for Nested BBCode

开发者 https://www.devze.com 2022-12-09 03:57 出处:网络
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'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.

0

精彩评论

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

关注公众号