Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex: check if URL is of facebook profile or page

Tags:

java

regex

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

like image 532
vikas devde Avatar asked Dec 16 '22 16:12

vikas devde


1 Answers

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

like image 74
Aviram Segal Avatar answered Jan 05 '23 11:01

Aviram Segal