Why does PathMatcher doesn't match path?

2019-09-09 16:30发布

问题:

I research glob patterns.

I wrote simple example:

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:D:\\folder1\\folder2\\**");
boolean isMatches  = matcher.matches(Paths.get("D:\\folder1\\folder2\\folder3"));
System.out.println(isMatches);

This code returns false.

If I use one star in pattern - I see same result.

What do I wrong?

回答1:

Try with \\\\ in path expression, to escape directory and reg expression

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:D:\\\\dev\\\\server\\\\**");
boolean isMatches  = matcher.matches(Paths.get("D:\\dev\\server\\web"));
System.out.println(isMatches);