Regular expressions: Finding BB code in a piece of text

非 Y 不嫁゛ 提交于 2020-01-04 15:33:10

问题


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?


回答1:


The following expression should do it for you

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



回答2:


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

Will pull both results.



来源:https://stackoverflow.com/questions/2008702/regular-expressions-finding-bb-code-in-a-piece-of-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!