What Is a Glob?
Two methods in theFiles class accept a glob argument, but what is a glob?You can use glob syntax to specify pattern-matching behavior.
A glob pattern is specified as a string and is matched against other strings, such as directory or file names. Glob syntax follows several simple rules:
- An asterisk,
*, matches any number of characters (including none). - Two asterisks,
**, works like*but crosses directory boundaries. This syntax is generally used for matching complete paths. - A question mark,
?, matches exactly one character. - Braces specify a collection of subpatterns. For example:
{sun,moon,stars}matches "sun", "moon", or "stars."{temp*,tmp*}matches all strings beginning with "temp" or "tmp."
- Square brackets convey a set of single characters or, when the hyphen character (
-) is used, a range of characters. For example:[aeiou]matches any lowercase vowel.[0-9]matches any digit.[A-Z]matches any uppercase letter.[a-z,A-Z]matches any uppercase or lowercase letter.
*,?, and\match themselves. - All other characters match themselves.
- To match
*,?, or the other special characters, you can escape them by using the backslash character,\. For example:\\matches a single backslash, and\?matches the question mark.
getPathMatcher
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*[0-9]*");
pathMatcher.matches(path)
Here are some examples of glob syntax:
*.html– Matches all strings that end in .html???– Matches all strings with exactly three letters or digits*[0-9]*– Matches all strings containing a numeric value*.{htm,html,pdf}– Matches any string ending with .htm, .html or .pdfa?*.java– Matches any string beginning witha, followed by at least one letter or digit, and ending with .java{foo*,*[0-9]*}– Matches any string beginning with foo or any string containing a numeric value
When the syntax is "
glob" then the String representation of the path is matched using a limited pattern language that resembles regular expressions but with a simpler syntax. For example:
*.javaMatches a path that represents a file name ending in .java*.*Matches file names containing a dot *.{java,class}Matches file names ending with .javaor.classfoo.?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:\\\\*")
The following rules are used to interpret glob patterns:
- The
*character matches zero or morecharactersof anamecomponent without crossing directory boundaries. - The
**characters matches zero or morecharacterscrossing directory boundaries. - The
?character matches exactly one character of a name component. - The backslash character (
\) is used to escape characters that would otherwise be interpreted as special characters. The expression\\matches a single backslash and "\{" matches a left brace for example. - The
[ ]characters are a bracket expression that match a single character of a name component out of a set of characters. For example,[abc]matches"a","b", or"c". The hyphen (-) may be used to specify a range so[a-z]specifies a range that matches from"a"to"z"(inclusive). These forms can be mixed so [abce-g] matches"a","b","c","e","f"or"g". If the character after the[is a!then it is used for negation so[!a-c]matches any character except"a","b", or"c".
Within a bracket expression the*,?and\characters match themselves. The (-) character matches itself if it is the first character within the brackets, or the first character after the!if negating. - The
{ }characters are a group of subpatterns, where the group matches if any subpattern in the group matches. The","character is used to separate the subpatterns. Groups cannot be nested.
沒有留言:
張貼留言