J0t4
J0t4

Reputation: 38

Java: RegEx to check for http links in a String

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

Answers (1)

Maria Ines Parnisari
Maria Ines Parnisari

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

Related Questions