Monday, February 21, 2011

Regular expressions: Finding BB code in a piece of text

I'm trying to match on "url" BB code tag in a random piece of text. Example text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. [url]http://www.google.com[/url] Donec purus nunc, rhoncus vitae tempus vitae, [url=www.facebook.com]facebook[/url] elementum sit amet justo.

I want to find both "url" tags from this text:

  • [url]http://www.google.com[/url]
  • [url=www.facebook.com]facebook[/url]

I am not that good with regular expressions so this is as far as I could get:

\[url(=[a-z]*)?\][a-z]*\[/url\]

I think I just need to replace [a-z] with something that matches on anything EXCEPT the characters '[' and ']'. Can anybody help me out with this please?

From stackoverflow
  • ((\[url\].*?\[/url\])|(\[url=.*\](.*?)\[/url\]))

    Will pull both results.

  • The following expression should do it for you

    \[url(=(.*?))?\](.*?)\[\/url\]
    

0 comments:

Post a Comment