1

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.

2
  • possible duplicate of RegEx match open tags except XHTML self-contained tags Commented Jan 18, 2014 at 3:09
  • No, not really, the regex is quite different, I don't want to extract any html tag links in source coder, rather http links in any sentence in plain text, quite diffetent. Commented Jan 18, 2014 at 3:23

1 Answer 1

1
.*(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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.