![](https://csdnimg.cn/release/download_crawler_static/3113969/bg14.jpg)
can understand any regular expression. It's not unusual to see a hundred line C or Java program expressed with
a one-line regular expression in Perl. That regex may be a little harder to understand than any single line out of
the longer program; on the other hand, the regex will likely be much easier to understand than the longer
program taken as a whole. You just have to keep these things in perspective.
5.1. The Regular Expression Bestiary
Before we dive into the rules for interpreting regular expressions, let's see what some patterns look like. Most
characters in a regular expression simply match themselves. If you string several characters in a row, they must
match in order, just as you'd expect. So if you write the pattern match:
/Frodo/
you can be sure that the pattern won't match unless the string contains the substring "Frodo" somewhere. (A
substring is just a part of a string.) The match could be anywhere in the string, just as long as those five
characters occur somewhere, next to each other and in that order.
Other characters don't match themselves, but "misbehave" in some way. We call these metacharacters. (All
metacharacters are naughty in their own right, but some are so bad that they also cause other nearby characters
to misbehave as well.)
Here are the miscreants:
\ | ( ) [ { ^ $ * + ? .
Metacharacters are actually very useful and have special meanings inside patterns; we'll tell you all those
meanings as we go along. But we do want to reassure you that you can always match any of these twelve
characters literally by putting a backslash in front of it. For example, backslash is itself a metacharacter, so to
match a literal backslash, you'd backslash the backslash:
\\.
You see, backslash is one of those characters that makes other characters misbehave. It just works out that
when you make a misbehaving metacharacter misbehave, it ends up behaving--a double negative, as it were. So
backslashing a character to get it to be taken literally works, but only on punctuational characters; backslashing
an (ordinarily well-behaved) alphanumeric character does the opposite: it turns the literal character into
something special. Whenever you see such a two-character sequence:
\b \D \t \3 \s
you'll know that the sequence is a metasymbol that matches something strange. For instance, \b matches a
word boundary, while
\t matches an ordinary tab character. Notice that a tab is one character wide, while a
word boundary is zero characters wide because it's the spot between two characters. So we call
\b a zero-width
assertion. Still,
\t and \b are alike in that they both assert something about a particular spot in the string.
Whenever you assert something in a regular expression, you're just claiming that that particular something has
to be true in order for the pattern to match.
Most pieces of a regular expression are some sort of assertion, including the ordinary characters that simply
assert that they match themselves. To be precise, they also assert that the next thing will match one character
later in the string, which is why we talk about the tab character being "one character wide". Some assertions
(like
\t) eat up some of the string as they match, and others (like \b) don't. But we usually reserve the term
"assertion" for the zero-width assertions. To avoid confusion, we'll call the thing with width an atom. (If you're
a physicist, you can think of nonzero-width atoms as massive, in contrast to the zero-width assertions, which
are massless like photons.)
You'll also see some metacharacters that aren't assertions; rather, they're structural (just as braces and
semicolons define the structure of ordinary Perl code, but don't really do anything). These structural
metacharacters are in some ways the most important ones because the crucial first step in learning to read
regular expressions is to teach your eyes to pick out the structural metacharacters. Once you've learned that,
reading regular expressions is a breeze.[1]
[1]Admittedly, a stiff breeze at times, but not something that will blow you away.