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


Chapter 7 Review of Regular Expression Matching



Yüklə 397,03 Kb.
Pdf görüntüsü
səhifə5/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

166
Chapter 7
Review of Regular Expression Matching
While there are several steps to using regular expressions in Python, each 
step is fairly simple.
1. Import the regex module with 
import re
.
2. Create a 
Regex
object with the 
re.compile()
function. (Remember to use 
a raw string.)
3. Pass the string you want to search into the 
Regex
object’s 
search()
method. This returns a 
Match
object.
4. Call the 
Match
object’s 
group()
method to return a string of the actual 
matched text.
N O T E
 
While I encourage you to enter the example code into the interactive shell, you 
should also make use of web-based regular expression testers, which can show you 
exactly how a regex matches a piece of text that you enter. I recommend the tester 
at https://pythex.org/.
More Pattern Matching with Regular Expressions
Now that you know the basic steps for creating and finding regular expres-
sion objects using Python, you’re ready to try some of their more powerful 
pattern-matching capabilities.
Grouping with Parentheses
Say you want to separate the area code from the rest of the phone number. 
Adding parentheses will create groups in the regex: 
(\d\d\d)-(\d\d\d-\d\d\
d\d)
. Then you can use the 
group()
match object method to grab the match-
ing text from just one group.
The first set of parentheses in a regex string will be group 
1
. The sec-
ond set will be group 
2
. By passing the integer 
1
or 
2
to the 
group()
match 
object method, you can grab different parts of the matched text. Passing 
0
or nothing to the 
group()
method will return the entire matched text. 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 number is 415-555-4242.')
>>> mo.group(1)
'415'
>>> mo.group(2)
'555-4242'
>>> mo.group(0)
'415-555-4242'
>>> mo.group()
'415-555-4242'
If you would like to retrieve all the groups at once, use the 
groups()
method—note the plural form for the name.


Pattern Matching with Regular Expressions

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