2016-06-09 128 views
1

讓我們:爲什麼glob:*匹配PathMatcher中的任何路徑?

Path path = Paths.get("C:\\1.txt"); 

下面的代碼打印 「真」:

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("regex:.*"); 
System.out.println(matcher.matches(path)); 

,但下面的代碼打印出 「假」:

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*"); 
System.out.println(matcher.matches(path)); 

爲什麼?

我期待在兩種方法中都有true

Glob page from Wikipedia,通配符*表示:

任意數目的字符,包括無

詳細匹配:

  • 的Java 8
  • 的Windows 7
+1

與其查閱維基百科關於glob的含義,我強烈建議[查看'getPathMatcher']的文檔(https://docs.oracle.com/javase/7/docs/api/爪哇/ NIO /文件/ FileSystem.html#getPathMatcher(java.lang.String中))。但是它也有類似的說法:*「\ *字符與名稱組件的零個或多個字符匹配,不會跨越目錄邊界。」*另請注意:*「\ * \ *字符匹配零個或多個跨越目錄邊界的字符。」*也許你想'**'(不知道,從來沒有使用'PathMatcher')。 –

+0

此外,您從Wikipage鏈接:COMMAND.COM和cmd.exe具有大多數常見語法有一些限制:沒有[...]和*可能只出現在模式的結尾,而不是# –

+0

@JorgeCampos:我的Windows 8系統上的'cmd.exe'在模式開始處用'*'表示很好。 –

回答

1

正如@TJ克羅德說,你應該好這一去:

PathMatcher matcher2 = FileSystems.getDefault().getPathMatcher("glob:**"); 
System.out.println(matcher2.matches(path)); 

欲瞭解更多信息請參閱this它說:

的**字符匹配零個或多個字符,穿越目錄 界限。

相關問題