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.

sundeep
  • 601
  • 1
  • 8
  • 21
  • http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – plsgogame Dec 23 '13 at 11:15

2 Answers2

5

you can check without regex in android

android.util.Patterns.WEB_URL.matcher(linkUrl).matches();
Nambi
  • 11,944
  • 3
  • 37
  • 49
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"
Community
  • 1
  • 1
marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33