I'm writing my own bbcode functions, and I'm pretty much done on that one. URL's are replaced if a user used [ url ]www.google.com[ /url ] or if a user used [ url=www.google.nl ]click here![ /url ]. Now comes the hard part: if a user didn't use that, and for example only typed www. google. be, I want to replace that as well. I tried to use the regex in the code box below, but that also replaces my already converted URL's...
There are many good ways to write parsers, but putting dozens of generalized regular expressions into your code is sure to be a slow and buggy solution, because you're constantly coding around all of these corner cases etc. My advice is to approach the problem from an entirely different direction. Write a proper parser; read each character in the input sequentially, and look for special input. If you see something interesting (say a [url] tag), switch your parser into a different context where it will react appropriately.
This is not easy; I'd recommend taking a look at some of the existing code out there if you want to write a robust solution.
As a solution to your actual problem, look into 'negative lookbehinds' in the Perl regular expression syntax. You want something like '(?<!\[url[=\]])' in front of your main pattern match.
Thanks for your reply. I'm looking into negative lookbehinds right now.
As for the regex you posted: I already have the [ url ] bbcode covered. I'm now looking into a way of automatically making urls clickable (say, if a user forgot to put the [ url ] bbcode in). So the html the regex needs to work on is the following: