Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate URL in java using regex? [duplicate]

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?

like image 339
psisodia Avatar asked Mar 20 '13 08:03

psisodia


People also ask

How do you check if a URL is valid or not in Java?

We can use java. net. url class to validate a URL. The idea is to create a URL object from the specified String representation.

What is a good regex to match a URL?

@:%_\+~#= , to match the domain/sub domain name.

How do I check if a URL is valid?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.


1 Answers

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"); 
like image 141
Tareq Avatar answered Sep 29 '22 06:09

Tareq