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.