javaregexurl

Java regex for URL


I need an regex, which will match the next urls:

(http|https)://(www)<my domain here>
(http|https)://(www)<my domain here>/page1
(http|https)://(www)<my domain here>/page1/.../
(http|https)://(www)<my domain here>/page1/...?a=b
(http|https)://(www)<my domain here>/page1/...?a=b&c=d...

I have one regex, but I don't know how to edit it

^(http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?$

Solution

  • You can use the following:

    ^(http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?(\/[a-z0-9])*(\/?|(\?[a-z0-9]=[a-z0-9](&[a-z0-9]=[a-z0-9]*)?))$
    

    For a specific domain name:

    ^(http:\/\/|https:\/\/)?(www.)?example\.com(\/?|(\?[a-z0-9]=[a-z0-9](&[a-z0-9]=[a-z0-9]*)?))$
    

    Edit: Validate domain and get Url:

    (http:\/\/|https:\/\/)?(www.)?example\.com\S*