11

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?

RobyJas
  • 75
  • 5
psisodia
  • 989
  • 4
  • 15
  • 36
  • 1
    Why not just try to establish an [`URL`](http://docs.oracle.com/javase/7/docs/api/java/net/URL.html) & connect to it? Best test ever. – Andrew Thompson Mar 20 '13 at 08:46
  • 4
    Duplicate? [http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Alpay Mar 20 '13 at 08:47
  • 1
    Refer this link: [Regular expresion to match URLs in Java][1] Hope it helps you. [1]: http://stackoverflow.com/questions/163360/regular-expresion-to-match-urls-in-java – Bhavesh Shah Mar 20 '13 at 08:52
  • Existing regular expression is not suited for me. That's y I have asked here. – psisodia Mar 20 '13 at 13:13

2 Answers2

17

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"); 
Tareq
  • 719
  • 1
  • 10
  • 22
8

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.

s_bei
  • 11,895
  • 8
  • 47
  • 67