【问题标题】:Why does PathMatcher doesn't match path?为什么 PathMatcher 不匹配路径?
【发布时间】:2014-10-05 06:15:40
【问题描述】:

我研究全局模式。

我写了一个简单的例子:

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

此代码返回false。

如果我在模式中使用一颗星 - 我会看到相同的结果。

我做错了什么?

【问题讨论】:

    标签: java path match nio glob


    【解决方案1】:

    尝试在路径表达式中使用\\\\,以转义目录和reg表达式

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

    【讨论】:

    • 哦,有趣)但是 4 个斜线看起来很糟糕)
    • PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:D:/folder1/folder2/*"); 这个变体也可以使用
    • @gstackoverflow 好点你的评论,它也有效。使用 `\\\\` 是 reg exp 方式:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多