问题
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