Problem Formulation
Say, you want to find a regex pattern in a given string. You know the pattern exists in the string. You use the re.match(pattern, string)
function to find the match object where the pattern matches in the string.
π¬ Problem: The Python regular expression pattern is not found in the string. The pattern doesn’t match anything, and, thus, the match object is None
. How to fix this?
Here’s an example in which you’re searching for the pattern 'h[a-z]+'
which should match the substring 'hello'
.
But it doesn’t match! β‘
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.match(pattern, my_string) if match: print('found!') else: print('not found!')
Output:
not found!
Where is the bug? And how to fix it, so that the pattern matches the substring 'hello'
?
π‘ Learn More: Improve your regex superpower by studying character classes used in the example pattern 'h[a-z]+'
by visiting this tutorial on the Finxter blog.
Solution: Use re.search() instead of re.match()
A common reason why your Python regular expression pattern is not matching in a given string is that you mistakenly used re.match(pattern, string)
instead of re.search(pattern, string)
or re.findall(pattern, string)
. The former attempts to match the pattern
at the beginning of the string
, whereas the latter two functions attempt to match anywhere in the string.
Here’s a quick recap of the three regex functions:
re.match(pattern, string)
returns a match object if thepattern
matches at the beginning of thestring
. The match object contains useful information such as the matching groups and the matching positions.re.search(pattern, string)
matches the first occurrence of thepattern
in thestring
and returns a match object.re.findall(pattern, string)
scansstring
from left to right, searching for all non-overlapping matches of thepattern
. It returns a list of strings in the matching order when scanning the string from left to right.
Thus, the following code uses re.search() to fix our problem:
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.search(pattern, my_string) if match: print('found!') else: print('not found!')
Output:
found!
Finally, the pattern 'h[a-z]+'
does match the string 'hello world'
.
Note that you can also use the re.findall() function if you’re interested in just the string matches of your pattern (without match object). We’ll explain all of this — re.match(), re.search(), re.findall(), and match objects — in a moment but first, let’s have a look at the same example with re.findall():
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.findall(pattern, my_string) print(match) # ['hello'] if match: print('found!') else: print('not found!')
Output:
['hello']
found!
Understanding re.match()
The re.match(pattern, string)
method returns a match object if the pattern
matches at the beginning of the string
. The match object contains useful information such as the matching groups and the matching positions. An optional argument flags
allows you to customize the regex engine, for example to ignore capitalization.
Specification:
re.match(pattern, string, flags=0)
The re.match()
method has up to three arguments.
pattern
: the regular expression pattern that you want to match.string
: the string which you want to search for the pattern.flags
(optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.
We’ll explore them in more detail later.
Return Value:
The re.match()
method returns a match object. You may ask (and rightly so):
π‘ Learn More: Understanding re.match()
on the Finxter blog.
What’s a Match Object?
If a regular expression matches a part of your string, there’s a lot of useful information that comes with it: what’s the exact position of the match? Which regex groups were matched—and where?
The match object is a simple wrapper for this information. Some regex methods of the re package in Python—such as search()
—automatically create a match object upon the first pattern match.
At this point, you don’t need to explore the match object in detail. Just know that we can access the start and end positions of the match in the string by calling the methods m.start()
and m.end()
on the match object m
:
>>> m = re.search('h...o', 'hello world') >>> m.start() 0 >>> m.end() 5 >>> 'hello world'[m.start():m.end()] 'hello'
In the first line, you create a match object m by using the re.search()
method. The pattern 'h...o'
matches in the string 'hello world'
at start position 0.
You use the start and end position to access the substring that matches the pattern (using the popular Python technique of slicing).
Now that you understood the purpose of the match object, let’s have a look at the alternative to the re.match()
function next! π
Understanding re.search()
The re.search(pattern, string)
method matches the first occurrence of the pattern
in the string
and returns a match object.
Specification:
re.search(pattern, string, flags=0)
The re.search()
method has up to three arguments.
pattern
: the regular expression pattern that you want to match.string
: the string which you want to search for the pattern.flags
(optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.
We’ll explore them in more detail later.
Return Value:
The re.search()
method returns a match object. You may ask (and rightly so):
π‘ Learn More: Understanding re.search()
on the Finxter blog.
Understanding re.findall()
The re.findall(pattern, string)
method scans string
from left to right, searching for all non-overlapping matches of the pattern
. It returns a list of strings in the matching order when scanning the string from left to right.

Specification:
re.findall(pattern, string, flags=0)
The re.findall()
method has up to three arguments.
pattern
: the regular expression pattern that you want to match.string
: the string which you want to search for the pattern.flags
(optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.
We will have a look at each of them in more detail.
Return Value:
The re.findall()
method returns a list of strings. Each string element is a matching substring of the string argument.
π‘ Learn More: Understanding re.findall()
on the Finxter blog.
Python Regex Course
Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.Β Β
Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.Β
Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.Β Β Regular expressions βrule the game βwhen text processing βmeets computer science.Β
If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet:
Now, this was a lot of theory! Let’s get some practice.
In my Python freelancer bootcamp, I’ll train you on how to create yourself a new success skill as a Python freelancer with the potential of earning six figures online.
The next recession is coming for sure, and you want to be able to create your own economy so that you can take care of your loved ones.
Check out my free “Python Freelancer” webinar now!
Join 20,000+ ambitious coders for free!