3 Minutes Python | Regex (Part 2)

Jay Shi
One Bit At A Time
Published in
3 min readJan 11, 2020

How to use regex in Python?

Photo by Evgeni Tcherkasski on Unsplash

From part 1, we understand how to write some simple regular expressions. But how do we actually use it in Python? This post will focus on different ways of using it in Python.

Introduce Python Regex Module — re

To use regular expression in Python, a module called re needs to be imported.

import re

There are a few different methods we can use in re module.

import re# re module function contractsre.match(regex_pattern, string_to_search)re.fullmatch(regex_pattern, string_to_search)re.search(regex_pattern, string_to_search)re.findall(regex_pattern, string_to_search)

Examples

Now let’s see some examples where we use those functions

Match phone number

Let’s say now we need to validate if a string is a 10-digit phone number in format ‘XXX-XXX-XXXX’, we can do the following:

import re# re.fullmatch(regex, str) checks if the whole string match the
# regex pattern. If the string does match, it returns a match
# object, otherwise it returns None
# this returns a match object, since the string is indeed 10-digits
# phone number.
re.fullmatch(r'\d{3}-\d{3}-\d{4}', '123-435-6454')
# this returns a None
re.fullmatch(r'\d{3}-\d{3}-\d{4}', '123-435-64544')

In our program, we can handle the regex conditional statement as follows:

import re
if re.fullmatch(regex, str):

# do something when the whole string matches
pass
else:
# do something when the whole string does not match
pass

Search for names in a Text

Let’s say now we are trying to search a name from a string. The first name starts with Mi, and the last name starts with Ch. We can do the following:

import re# re.search(regex, string_to_search) searches the string for the
# first location where it's matching the regular expression.
# It returns match object if there is a match, otherwise it returns
# a None.
# match_result now is evaluated as a match since there is a match
match_result = re.search(
r'Mi[a-z]+\sCh[a-z]+',
'Lorem ipsum dolor sit amet, Mike Chang elit.'
)
# To get the matched string, we can use group. This prints out
# Mike Chang.
print(match_result.group())
# match_result_2 now is evaluated as None since there is no match
match_result_2 = re.search(
r'Mi[a-z]+\sCh[a-z]+',
'Lorem ipsum dolor sit amet, Mike Chang elit.'
)

Findall and match

These two methods have a very similar use as re.search and re.match.

import re# re.findall(regex, string_to_search) finds all matching substrings 
# and return them as a list. If there is no match, it returns a
# empty list.
# this returns ['a', 'a', 'a', 'a']
re.findall(r'a', 'aaaa')
# this returns []
re.findall(r'a', 'bbbb')
# re.match(regex, string_to_search) search the string from the
# beginning, returns a match object if there is a match, otherwise
# returns None.
# This returns None since there is no match at the beginning of the
# of the string.
re.match(r'ab', 'cab')
# This returns a match since there is a match at the beginning
re.match(r'ab', 'abc')

Recap

To use regex in Python, the module re is needed.

Four main ways to search/match strings are search(), fullmatch(), match(), and findall(). For these four methods, all of them return a match object if succeed except findall(), which returns a list of match strings.

Previous Python Posts

--

--

Jay Shi
One Bit At A Time

Software Engineer @ Google. I write about Python tutorials and stuff that can help you become a better software engineer.