开发者

Regular expressions: Finding BB code in a piece of text

I'm tryi开发者_Python百科ng 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?


The following expression should do it for you

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


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

Will pull both results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜