I’m trying to write a simple localized URL detection module, I’m using https://regexr.com/ to test my message as below
It’s working fine and so I’m replicating it to Java console program –
public static void main(String[] args) throws MalformedURLException, IOException, NoSuchAlgorithmException {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String file = "<path to a file>";
String regex = "(.*)?([A-Za-z0-9-]{2,})+([.])+(com|co|link|ly|me|my|sg|xyz)+";
Pattern pattern = Pattern.compile(regex);
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
line = line.trim().toLowerCase();
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
System.out.println(simpleDateFormat.format(new Date()) + " HIT - " + line);
} else {
System.out.println(simpleDateFormat.format(new Date()) + " NOT - " + line);
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception exception) {
exception.printStackTrace(System.out);
}
}
Unfortunately this program prints –
2024-06-24 00:07:04.980 NOT – — a simple message with google.com url in it.
What is the difference of regex between these 2 platforms?