Pattern Matching with Regular Expressions
181
The
TODO
comments are just a skeleton for the program. They’ll be
replaced as you write the actual code.
The phone number begins with an
optional area code, so the area code
group is followed with a question mark. Since the
area code can be just three
digits (that is,
\d{3}
)
or three digits within parentheses (that is,
\(\d{3}\)
),
you should have a pipe joining those parts. You can add the regex comment
# Area code
to this part of the multiline string
to help you remember what
(\d{3}|\(\d{3}\))?
is supposed to match.
The phone number separator character can be a space (
\s
), hyphen (
-
),
or period (
.
), so these parts should also be joined by pipes. The next few
parts of the regular expression are straightforward: three digits, followed
by another separator, followed by four digits. The
last part is an optional
extension made up of any number of spaces followed by
ext
,
x
, or
ext.
, fol-
lowed by two to five digits.
N O T E
It’s easy to get mixed up with regular expressions that contain groups with parenthe-
ses
( )
and escaped parentheses
\( \)
. Remember to double-check that you’re using
the correct one if you get a “missing ), unterminated subpattern” error message.
Dostları ilə paylaş: