tags:

views:

111

answers:

1

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=extra]sdfsdf[/b]

[b=extra]left[u]middle[/u]right[/b]
+1  A: 

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.

SLaks
Why was this downvoted?
SLaks