In Java 8, the following script prints true
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String regex = "^(/[^/[a-zA-Z0-9_.-]+]*)+/?$";
String inString = "/tmp"; // the same goes for e.g. /usr/pgsql-15
Matcher matcher = Pattern.compile(regex).matcher(inString);
System.out.println("Matches regex: " + matcher.matches());
}
}
In Java 21, however, it prints false
. Why?
Could this be related to JDK-6609854?
It turns out that this is a bug from Java 8 (link). Also see this mailing discussion.
The solution is to update the regex, for example:
^(/[-a-zA-Z0-9_.]+)+/?$