Hao Ting Hao Ting -4 years ago 155
Java Question

Website/URL Validation Regex in JAVA

I need a regex string to match URL starting with "http://", "https://", "www.", "google.com"

the code i tried using is:

//Pattern to check if this is a valid URL address
Pattern p = Pattern.compile("(http://|https://)(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?");
Matcher m;

but this code only can match url such as "http://www.google.com"

I know this ma be a dupicate question but i have tried all of the regex provided and it does not suit my requirement. Willl someone please help me? Thank you.

Answer Source

You need to make (http://|https://) part in your regex as optional one.



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download