I want to check validate URL allowing wildcard in java.
I found some nice examples about validating URL in java (REGEX, urlValidator), but those aren't providing the wildcard character.
Here's what I'm practicing:
CODE(urlValidator)
public void urlValidiTest(){
System.out.println(this.urlCheck("https://www.google.com"));
System.out.println(this.urlCheck("https://google.com"));
System.out.println(this.urlCheck("*.com"));
}
public boolean urlCheck(String url){
return new UrlValidator().isValid(url);
}
OUTPUT
true
true
false
CODE(regex)
public void regexTest() {
String[] URLs = new String[] { "http://www.google.com", "http://google.com/","*.com" };
Pattern REGEX = Pattern.compile("(?i)^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$");
for (String url : URLs) {
Matcher matcher = REGEX.matcher(url);
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}
RESULT
What I want to do is all the above URL is valid.
How should I approach this problem to solve?
Any comment would be appreciated. Thanks.
UPDATES
I got rid of the scheme part and added |* and |\.* to the domain part following the answer(|* and |.* gives me a error - invalid escape sequence(valid ones are \b \t \n \f \r \" \' ) - but I'm not sure the changes are right).
Now it doesn't allow "google.com"; but allow others("www.google.com", "google.com", ".google.com", ".com")
public void regexValidator(String str){
Pattern REGEX = Pattern.compile(""
+ "(?i)^(?:\\S+(?::\\S*)?@)"
+ "?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)"
+ "(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
+ "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|"
//DOMAIN
+ "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+|\\*)"
+ "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"
//
+ "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)"
+ "(?::\\d{2,5})?(?:[/?#]\\S*)?$");
Matcher _matcher = REGEX.matcher(str);
if(_matcher.find()){
System.out.println("[O] " + str);
}
else {
System.out.println("[X]" + str);
}
}
public void validate(){
System.out.println("TEST START");
this.regexValidator("https://www.google.com");
this.regexValidator("www.google.com");
this.regexValidator("google.com");
this.regexValidator("*.google.com");
this.regexValidator("*.com");
System.out.println("DONE");
}
TEST START
[O] www.google.com
[O] google.com
[O] *.google.com
[O] *.com
DONE
Need any help. Thanks.