Wildcard Metasymbols
Three special metasymbols serve as generic wildcards, each of them matching “any”
character (for certain values of “any”). These are the dot (“.”), \C,
and \X. None of these may be used in
their metacharacter sense in a bracketed character class. You can’t use
the dot there because it would match (nearly) any character in
existence, so it’s something of a universal character class in its own
right. If you’re going to include or exclude everything, there’s not
much point in having a bracketed character class. The special wildcards
\C and \X have special structural meanings that don’t
map well to the notion of choosing a single Unicode character, which is
the level at which bracketed character classes work.
The dot metacharacter matches any one character other than a
newline. (And with the /s modifier,
it matches that, too. In which case use \N to match a non-newline.) Like any of the
dozen special characters in a pattern, to match a dot literally, you
must escape it with a backslash. For example, this checks whether a
filename ends with a dot followed by a one-character extension:
if ($pathname =~ /\.(.)\z/s) {
say "Ends in $1";
}The first dot, the escaped one, is the literal character, and the
second says “match any character”. The \z says to match only at the end of the
string, and the /s modifier lets the
dot match a newline as well. (Yes, using a newline as a file extension
Isn’t Very Nice, but that doesn’t mean it can’t happen.)
The dot metacharacter is most ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access