How to validate URL in java using regex?

32,870

Solution 1

This works:

Pattern p = Pattern.compile("(@)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");  

    Matcher m = p.matcher("your url here"); 

Solution 2

I am use the following code for that

String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

btw a search in google and you would find the solution by yourself.

Share:
32,870
psisodia
Author by

psisodia

Updated on September 14, 2020

Comments

  • psisodia
    psisodia over 3 years

    I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?

  • psisodia
    psisodia about 11 years
    it is not checking for www if I write www.abc.com
  • Honza
    Honza over 10 years
    psisodia: "www" is not necessary in URL address
  • user3145373 ツ
    user3145373 ツ about 10 years
    @Stefan Beike : Shall it will check for 192.168.67.90/mysite/page1.html url ?