sundeep
sundeep

Reputation: 601

how to find if the input text is a url using regex

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.

Upvotes: 3

Views: 914

Answers (2)

marcus.ramsden
marcus.ramsden

Reputation: 2633

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"

Upvotes: 4

Nambi
Nambi

Reputation: 12042

you can check without regex in android

android.util.Patterns.WEB_URL.matcher(linkUrl).matches();

Upvotes: 5

Related Questions