Greetings all. I am using the following regex to detect urls in a string and wrap them inside the < a > tag
public static String detectUrls(String text) {
String newText = text
.replaceAll("(?:https?|ftps?|http?)://[\\w/%.-?&=]+",
"<a href='$0'>$0</a>").replaceAll(
"(www\\.)[\\w/%.-?&=]+", "<a href='http://$0'>$0</a>");
return newText;
}
i have a problem that the following links are not detected correctly: i am not that good with regex, so please advise.
http://code.google.com/p/shindig-dnd/
http://confluence.atlassian.com/display/GADGETDEV/Gadgets+and+JIRA+Portlets
www.liferay.com/web/raymond.auge/blog/
(www.opensocial.org/)
The problem you have is that you are using
-
within a character group ([]
) without escaping it, which is being used to define the range.-?
(i.e. the characters./0123456789:;<=>?
). Either escape it\\-
or put it at the end of the character class so that it doesn't complete a range.I'm using this:
As marcog said, you should escape the
-
and to match the last 2 examples you gave, you have to make thehttp
optionnal. Alsohttp?
matcheshtt
wich is not a correct protocol.So the regex will be: