I want to check if the string is the URL of facebook using java regex.
for eg: facebook.com/username
it should also match if it is with the protocol http or https
http://www.facebook.com/xyz
https://www.facebook.com/xyz
or without protocol also
www.facebook.com/xyz
I wouldn't use regex, create a new URL and use getHost to get the hostname and check if its ok for you. (also can check the other URL information)
if (!urlString.startsWith("http")) {
urlString = "http://" + urlString;
}
String hostname = new URL(urlString).getHost();
If you insist on using regex you can use the following:
String regex = "((http|https)://)?(www[.])?facebook.com/.+";
someURL.matches(regex);
Should match any combination of http
/https
with or without www
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With