+2  A: 

I was going to suggest a BBCodeParser...

I have also looked into PECL and Pear HTML_BBCodeParser. But i don't want my application to be dependant on extensions

I find that to be very strange. Why reinvent the wheel? One of the principles of good software-engineering is DRY (Don't Repeat Yourself). You're trying to solve a problem that has already been solved.

I like to do things on my own!

That's not bad in of itself, but there are times when you are better off using a tried and true solution; one that is better tested and more robust than your own (as you're finding out). That way you will spend time on the problem you actually want to solve instead of solving a problem that has already been solved. Don't fall into the trap of reinventing the wheel. :)

My suggestion (and solution) to you is to use a BBCode parser.

EDIT

Another thing is that you're parsing something that is HTML-like. Things of that nature don't lend themselves easily to being parsed by regular expressions.

Vivin Paliath
Well I was talking about server side (i mean it has to be installed by hosting company or server admin) extensions that php.net suggest. It is always better to have a standalone app that you can just upload to the host and it is ready to use.
Paul
This should be a comment since it doesn't answer the OP question.
Artefacto
I guess you missed this part: "My suggestion (and solution) to you is to use a BBCode parser.", and the part after the edit.
Vivin Paliath
fair enough (extra chars)
Artefacto
@Paul you can always ask the hosting company or server-admin to include that particular extension. There are many developer-friendly hosting-solutions. If that was not the case, you would have to rewrite every single extension!
Vivin Paliath
@Artefacto no worries :)
Vivin Paliath
+2  A: 

Your regex, especially the zero-width assertions (lookaround) cause the regex engine to backtrack catastrophically. Morale of the story: Regex shouldn't can't be used to parse languages that are not regular. If you have nested structures, that's not a regular language.

In fact, I think BBCode is evil. BBCode is a markup language invented by lazy programmers who didn't want to filter HTML the proper way. As a result, we now have a loose "standard" that's hard to implement. Filter your HTMl the right way:

http://htmlpurifier.org/

NullUserException
Hmm... Maybe you are right about using regex for such matter. Well HTML instead of BBCode would be great, but people are used to BBCode and it's like some kind of standart now so you cant throw it out.
Paul