64

直接来自Java Oracle 教程:

两个星号 ** 与 * 类似,但跨越目录边界。此语法通常用于匹配完整路径。

任何人都可以做一个真实的例子吗?“跨越目录边界”是什么意思?跨越目录边界,我想像检查文件从根目录到getNameCount()-1. 再举一个真实的例子来解释 * 和 ** 在实践中的区别会很棒。

4

2 回答 2

70

javadoc forFileSystem#getPathMatcher()有一些很好的例子和解释

*.java Matches a path that represents a file name ending in .java 
*.*    Matches file names containing a dot 

*.{java,class}  Matches file names ending with .java or .class 
foo.?           Matches file names starting with foo. and a single character extension 
/home/*/*       Matches /home/gus/data on UNIX platforms 
/home/**        Matches /home/gus and /home/gus/data on UNIX platforms 
C:\\*           Matches C:\foo and C:\bar on the Windows platform (note that the backslash is escaped; as a string literal in the Java Language the pattern would be "C:\\\\*")  

所以/home/**会匹配/home/gus/data,但/home/*不会。

/home/*直接在/home目录中说每个文件。

/home/**是说里面的任何目录中的每个文件/home


*与的例子**。假设您当前的工作目录是/Users/username/workspace/myproject,那么以下将只匹配./myproject文件(目录)。

PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/Users/username/workspace/*");
Files.walk(Paths.get(".")).forEach((path) -> {
    path = path.toAbsolutePath().normalize();
    System.out.print("Path: " + path + " ");
    if (pathMatcher.matches(path)) {
        System.out.print("matched");
    }
    System.out.println();
});

如果使用**,它将匹配该目录中的所有文件夹和文件。

于 2013-09-10T15:09:08.890 回答
18

Double asterisk (**) matches zero or more characters across multiple nested directories. I will explain the double asterisk as well as other wildcards that are useful step by step with examples after explaining the main concept.


Globbing

A glob is a string literal and/or wildcard characters used to match file paths. Locating files on a filesystem using one or more globs is called globbing. The globbing is not just limited to Java. It's also used for matching files in various configuration files, such as listing ignored files and directories in .gitignore in Git, selecting files and folders in Unix operating system, e.g ls **/*.java etc.

Following are some of the most important parts of globbing. Double asterisk(**) is one of them:


Separator and Segments (/)

In Globbing, the forward slash character (/) always acts as the separator, no matter what operating system is being used. A segment is everything that comes between the two separators.

Example: tests/HelloWorld.java

Here, tests and HelloWorld.java are the segments and / is the separator.


Single Asterisk (*)

Single Asterisk (*) matches zero or more characters within one segment. It is used for globbing the files within one directory.

Example: *.java

This glob will match files such as HelloWorld.java but not files like tests/HelloWorld.java or tests/ui/HelloWorld.java.


Double Asterisk (**)

Double Asterisk (**) matches zero or more characters across multiple segments. It is used for globbing files that are in nested directories.

Example: tests/**/*.java

Here, the file selecting will be restricted to the tests directory. The glob will match the files such as tests/HelloWorld.java, tests/ui/HelloWorld.java, tests/ui/feature1/HelloWorld.java.


Question Mark(?)

Question mark(?) matches a single character within one segment. It can be used for matching the files or folders that differ in name by just one character.

Example: tests/?at.java

This will match files such as tests/cat.java, test/Cat.java, test/bat.java etc.


Square Brackets ([abc])

Square Brackets ([...]) matches a single character given in the square brackets.

Example: tests/[CB]at.java

This glob will match files like tests/Cat.java or tests/Bat.java


Square Brackets Range ([a-z])

Square Brackets Range ([a-z]), matches one character given in the range.

Example: tests/feature[1-9]/HelloWorld.java

This glob will match files like tests/feature1/HelloWorld.java, test/feature2/HelloWorld.java and so on... upto 9.


Negation (!)

Negation (!) is used for excluding some files.

Example: tests/[!C]at.java

This will exclude the file tests/Cat.java and will match files like tests/Bat.java, tests/bat.java, tests/cat.java.


That's it! Hope that helps.

于 2020-07-20T16:08:08.127 回答