Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate a URL in android

Tags:

regex

android

How can I validate URL in android... I am trying to use regex but it is always returning false even if the url is correct

Code used:

private boolean checkEmail(String email) {
        return URL.matcher(email).matches();
}
    public final Pattern URL = Pattern.compile(
              "/((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?‌​(?:[\\w]*))?)/"
      );

I have got very basic knowledge in regex, so I don't know whether the above used string is correct. Please help...

like image 725
ganapathy Avatar asked Nov 15 '25 23:11

ganapathy


2 Answers

As your question is more specific to android. You can use this

android.util.Patterns.WEB_URL.matcher(url).matches();
like image 88
Ads Avatar answered Nov 18 '25 14:11

Ads


In case you want to find a URL inside a text comprised of string other than just the URL, use this

Patterns.WEB_URL.matcher(bodyText).find()

matches() tries to match the string length to exactly that of the URl, while find() will look for the string in the entire text

like image 23
Shubham Gupta Avatar answered Nov 18 '25 13:11

Shubham Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!