Reputation: 38
I need to check if there are any http link in a string, but I don't figure it out yet how to do this. I know I can check if a link is a valid http link with something like the following
boolean valid = myString.matches("^(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
I see it in this post here, it works if the string is just the link, without spaces and other words, but I want a regex to check for http links in a sentence, which could or not contains spaces and more words, I tried the next:
boolean valid = s.matches(".*\\s+^(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].*$")
But I don't get it, I don' really know much about regex, so I'd appreciate any help.
Thankz in advance.
Upvotes: 1
Views: 335
Reputation: 17486
.*(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].*$
Translated: (Any characters) hyperlink (Any characters)
You can test it here with some strings of your own.
Upvotes: 1