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?
Asked
Active
Viewed 3.3k times
11
-
1Why 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
-
4Duplicate? [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
-
1Refer 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 Answers
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
- 689
- 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.

nano_nano
- 12,351
- 8
- 55
- 83
-
3
-
2
-
@Stefan Beike : Shall it will check for `192.168.67.90/mysite/page1.html` url ? – user3145373 ツ Apr 25 '14 at 07:08