3

I want to check if the input received is url using REGEX. How can i do it ? Is there any standard REGEX available that works for checking url?

my requirement is to check if the input text is a url or not, but not if the text contains a url.

1

2 Answers 2

5

you can check without regex in android

android.util.Patterns.WEB_URL.matcher(linkUrl).matches();
Sign up to request clarification or add additional context in comments.

2 Comments

hey sorry for the delay in accepting the answer. thanks your solution works.
it's ok all the best :)
4

I would strongly recommend not using a Regex in this situation as there are a lot of different cases that can constitute a valid URL (see the top voted answer to this question for an example of an RFC valid regex). If working on Android I would recommend using the Uri class;

String urlToValidate = "http://google.co.uk/search";
Uri uri = Uri.parse(urlToValidate);

You can then look at the different parts of the URL to ensure that you are willing to accept the URL input. For example;

uri.getScheme(); // Returns "http", if you only want an HTTP url
uri.getHost(); // Returns "google.co.uk", if you only want a specific domain
uri.getPath(); // Returns "/search"

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.