Java Reference
In-Depth Information
Second, the pattern matching is done by the shell, the command inter-
preter,
before
the arguments are handed off to the specific command. Any text
with these special characters is replaced, by the shell, with one or more filenames
that match the pattern. This means that all the other Linux commands (
mv
,
cp
,
ls
, and so on) never see the special characters—they don't do the pattern
matching, the shell does. The shell just hands them a list of filenames.
The significance here is that this functionality is available to any and every
command, including shell scripts and Java programs that you write, with no
extra effort on your part. It also means that the syntax for specifying multiple
files doesn't change between commands—since the commands don't implement
that syntax; it's all taken care of in the shell before they ever see it. Any com-
mand that can handle multiple filenames on a command line can benefit from
this shell feature.
If you're familiar with MS-DOS commands, consider the way pattern
matching works (or doesn't work) there. The limited pattern matching you
have available for a
dir
command in MS-DOS doesn't work with other com-
mands—unless the programmer who wrote that command also implemented
the same pattern matching feature.
What are the other special characters for pattern matching with filenames?
Two other constructs worth knowing are the question mark and the square
brackets. The “
?
” will match any
single
character.
The
[...]
construct is a bit more complicated. In its simplest form, it
matches any of the characters inside; for example,
[abc]
matches any of
a
or
b
or
c
. So
Version[123].java
would match a file called
Version2.java
but not those called
Version12.java
or
VersionC.java
. The pattern
Version*.java
would match all of those. The pattern
Version?.java
would
match all except
Version12.java
, since it has two characters where the
?
matches only one.
The brackets can also match a range of characters, as in
[a-z]
or
[0-9]
.
If the first character inside the brackets is a “
^
” or a “
!
”, then (think “not”) the
meaning is reversed, and it will match anything but those characters. So
Version[^0-9].java
will match
VersionC.java
but not
Version1.java
.
How would you match a “
-
”, without it being taken to mean a range? Put it
first inside the brackets. How would you match a “
^
” or “
!
” without it being
understood as the “not”?
Don't
put it first.
Some sequences are so common that a shorthand syntax is included. Some
other sequences are not sequential characters and are not easily expressed as a
range, so a shorthand is included for those, too. The syntax for these special