I have following regular expression
(?i)\b((https?:\/\/www\.)|(https?:\/\/)|(www\.))?(localhost).*\b
and following url
http://localhost:8081/saman/ab/cde/fgh/ijkl.jsf?gdi=ff8081abcdef02a011b0af032170001&ci=
It matches when tried with both https://regex101.com/ and http://rubular.com/r/kyiKS9OlsM
But when there is any special character at the end, url does not match
import java.text.Format;
import java.text.MessageFormat;
import java.util.regex.Pattern;
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
private static final String URL_MATCH_REGEX = "(?i)\\b((https?:\\/\\/www\\.)|(https?:\\/\\/)|(www\\.))?({0}).*\\b";
private static final Format format = new MessageFormat(URL_MATCH_REGEX);
static String regex = "";
static String url = "http://localhost:8081/saman/ab/cde/fgh/ijkl.jsf?gdi=ff8081abcdef02a011b0af032170001&ci=";
public static void main(String[] args) {
try {
regex = format.format(new Object[]{replaceDomainToUseInRegex("localhost")});
System.out.println(regex);
Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher( url ).matches());
} catch (Exception e) {
}
}
private static String replaceDomainToUseInRegex(String domain) {
return domain.replace(".", "\\.").replace("/", "\\/").replace("?", "\\?");
}
}
Can anyone help me to figure out the issue here?