I am coding a bbcode parser system. But I'm stuck with the nested tags.
I would like to know if someone know the regex pattern to find for instance a [url] tag.
Example: [url][url]Hello world .com[/url][/url].
I want the output for this to be <a>[url]Hello world .com[/url]</a> where [url]Hello world.com [url/] becomes clickable.
Please not that "[url][url]Hello world[/url][/url] [url]Hello again .com[/url]" can occure and all other combinations are also possible.
If you have a better idea than doing this with regex, please let me know :)

Here's my normal regex pattern:
"#\[url\](.*)\[\/url\]#isU" //this is making the output <a>[url]Hello world .com</a>[/url], and it's logical but how to fix it ?
Thank you

Edited by cmps

3 Years
Discussion Span
Last Post by cmps

I'm not trying to shoot down your idea, but why not just use a bbcode parser that already exists (unless you are doing it to learn)? Here is a class someone had done and you could extend it if needed http://jbbcode.com/

Edited by pixelsoul


Thank you for this link, no in contrary that's just great. In fact I have implemented a function based on recursive backtracking to prevent nested tags but it needs more conditions for special cases, so Is wondered if a regex pattern exists to solve this. I haven't checked the code for this application jbbcode but I'll do 2mrw. Have a nice day!


If you are still looking for a regex, something like this might work:


It returns only [url] tags that are not followed by another [url] tag.


Hey minitauros, thank you for this regex pattern, it's useful I added the U at the end of the pattern since I'm gonna have more than one in an input. I am thinking of implementing a new one soon since I can't find one that is working 100% with no bugs or probs. Thank you guys you were helpful :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.