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

Recommended Answers

All 4 Replies

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/

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:

/\[url\](?!\[url\])(.*?)\[\/url\]/is

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.