167 >>> mo.groups() ('415', '555-4242')
>>> areaCode, mainNumber = mo.groups() >>> print(areaCode) 415
>>> print(mainNumber) 555-4242
Since
mo.groups()
returns a tuple of multiple values, you can use the
multiple-assignment trick to assign each value to a separate variable, as in
the previous
areaCode, mainNumber = mo.groups()
line.
Parentheses have a special meaning in regular expressions, but what
do you do if you need to match a parenthesis in your text? For instance,
maybe the phone numbers you are trying to match have the area code set
in parentheses. In this case, you need to escape the
(
and
)
characters with
a backslash. Enter the following into the interactive shell:
>>> phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)') >>> mo = phoneNumRegex.search('My phone number is (415) 555-4242.') >>> mo.group(1) '(415)'
>>> mo.group(2) '555-4242'
The
\(
and
\)
escape characters in the raw string passed to
re.compile()
will match actual parenthesis characters. In regular expressions, the follow-
ing characters have special meanings:
. ^ $ * + ? { } [ ] \ | ( )
If you want to detect these characters as part of your text pattern, you
need to escape them with a backslash:
\. \^ \$ \* \+ \? \{ \} \[ \] \\ \| \( \)
Make sure to double-check that you haven’t mistaken escaped parenthe-
ses
\(
and
\)
for parentheses
(
and
)
in a regular expression. If you receive
an error message about “missing )” or “unbalanced parenthesis,” you may
have forgotten to include the closing unescaped parenthesis for a group,
like in this example:
>>> re.compile(r'(\(Parentheses\)') Traceback (most recent call last):
--snip--
re.error: missing ), unterminated subpattern at position 0
The error message tells you that there is an opening parenthesis at
index
0
of the
r'(\(Parentheses\)'
string that is missing its corresponding
closing parenthesis.