7 pat t e r n m at c h I n g w I t h r e g u L a r e X p r e s s I o n s



Yüklə 397,03 Kb.
Pdf görüntüsü
səhifə6/25
tarix29.11.2022
ölçüsü397,03 Kb.
#71308
1   2   3   4   5   6   7   8   9   ...   25
P A T T E R N M A T C H I N G W I T H

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.



Yüklə 397,03 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   25




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin