I'm trying to match relative paths using a PathMatcher. A very common case is to use a pattern like **/foo.php to match all files named foo.php whatever is the folder they are in.
But I found a behavior that look wrong to me, when the file is actually in no folders:
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
public class PathMatcherTest {
public static void main(String[] args) {
testMatch("foo.php", Paths.get("foo.php"));
testMatch("foo.php", Paths.get("sub/foo.php"));
testMatch("**/foo.php", Paths.get("sub/foo.php"));
testMatch("**/foo.php", Paths.get("foo.php"));
}
private static void testMatch(String pattern, Path path) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
if (pathMatcher.matches(path)) {
System.out.println(pattern + " matches " + path);
} else {
System.out.println(pattern + " doesn't matches " + path);
}
}
}
produces:
foo.php matches foo.php // OK foo.php doesn't matches sub/foo.php // OK **/foo.php matches sub/foo.php // OK **/foo.php doesn't matches foo.php // KO, why ????
Why is the GLOB pattern **/foo.php not matching foo.php? Am I misreading the spec, or is it a bug?