Fundamentals 17 min read

Understanding Python's re Module and Regular Expressions

This article introduces Python's re module, explaining regular expression fundamentals, key functions such as match, search, compile, sub, findall, finditer, and split, detailing their syntax, parameters, flags, and providing numerous code examples to illustrate pattern matching, searching, replacing, and splitting strings.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Python's re Module and Regular Expressions

Regular expressions are special character sequences that help you conveniently check whether a string matches a certain pattern.

Since Python 1.5, the re module has been added, providing Perl-style regular expression patterns.

The re module gives Python full regular expression capabilities.

The compile function creates a regular expression object from a pattern string and optional flag arguments; the object has methods for matching and substitution.

The re module also offers functions that perform the same operations directly using a pattern string as the first argument.

>> re.match function

re.match attempts to match a pattern at the beginning of a string; if the match is not at the start, match() returns None .

<code>re.match(pattern, string, flags=0)</code>

Parameters:

If the match succeeds, re.match returns a match object; otherwise it returns None .

You can use group(num) or groups() on the match object to retrieve the matched expressions.

Example

<code>#!/usr/bin/pythonimport re
print(re.match('www', 'www.runoob.com').span())  # matches at start
print(re.match('com', 'www.runoob.com'))         # does not match at start</code>

Output:

<code>(0, 3)None</code>

Another example

<code>#!/usr/bin/python3import re
line = "Cats are smarter than dogs"
matchObj = re.match(r'(.*) are (.*?) .*', line, re.M|re.I)
if matchObj:
    print ("matchObj.group() : ", matchObj.group())
    print ("matchObj.group(1) : ", matchObj.group(1))
    print ("matchObj.group(2) : ", matchObj.group(2))
else:
    print ("No match!!")</code>

Output:

<code>matchObj.group() :  Cats are smarter than dogs
matchObj.group(1) :  Cats
matchObj.group(2) :  smarter</code>

>> re.search method

re.search scans the entire string and returns the first successful match.

Syntax:

<code>re.search(pattern, string, flags=0)</code>

Parameters:

If the match succeeds, re.search returns a match object; otherwise None .

You can use group(num) or groups() on the match object to retrieve the matched expressions.

>> Difference between re.match and re.search

re.match only matches at the start of the string; if the start does not satisfy the pattern, it fails and returns None . re.search scans the whole string until a match is found.

<code>#!/usr/bin/python3 import re
line = "Cats are smarter than dogs";
matchObj = re.match(r'dogs', line, re.M|re.I)
if matchObj:
    print ("match --> matchObj.group() : ", matchObj.group())
else:
    print ("No match!!")
matchObj = re.search(r'dogs', line, re.M|re.I)
if matchObj:
    print ("search --> matchObj.group() : ", matchObj.group())
else:
    print ("No match!!")</code>

Output:

<code>No match!!search --> matchObj.group() :  dogs</code>

>> Search and Replace

Python's re module provides re.sub to replace matched substrings in a string.

Syntax:

<code>re.sub(pattern, repl, string, count=0, flags=0)</code>

Parameters:

pattern: the regex pattern string.

repl: the replacement string or a function.

string: the original string to be processed.

count: maximum number of replacements (0 means replace all).

flags: optional matching flags.

First three parameters are required; the last two are optional.

<code>#!/usr/bin/python3import re
phone = "2004-959-559 # 这是一个电话号码"
# Remove comment
num = re.sub(r'#.*$', "", phone)
print ("电话号码 : ", num)
# Remove non-digits
num = re.sub(r'\D', "", phone)
print ("电话号码 : ", num)</code>

Output:

<code>电话号码 :  2004-959-559 电话号码 :  2004959559</code>

>> repl parameter can be a function

In the following example, matched numbers are multiplied by 2.

<code>#!/usr/bin/python import re
# Multiply matched numbers by 2
def double(matched):
    value = int(matched.group('value'))
    return str(value * 2)
s = 'A23G4HFD567'
print(re.sub('(?P<value>\d+)', double, s))</code>

Output:

<code>A46G8HFD1134</code>

>> compile function

The compile function compiles a regular expression into a pattern object for use with match() and search() .

Syntax:

<code>re.compile(pattern[, flags])</code>

Parameters:

pattern: a string representing the regular expression.

flags (optional): matching modes such as case‑insensitive, multiline, etc. Common flags include re.I (ignore case), re.L , re.M , re.S , re.U , re.X .

<code>&gt;&gt;&gt;import re&gt;&gt;&gt; pattern = re.compile(r'\d+')
&gt;&gt;&gt; m = pattern.match('one12twothree34four')        # no match at start
&gt;&gt;&gt; print m
None
&gt;&gt;&gt; m = pattern.match('one12twothree34four', 2, 10) # start at 'e', no match
&gt;&gt;&gt; print m
None
&gt;&gt;&gt; m = pattern.match('one12twothree34four', 3, 10) # start at '1', matches
&gt;&gt;&gt; print m
&lt;_sre.SRE_Match object at 0x10a42aac0&gt;
&gt;&gt;&gt; m.group(0)   # '12'
&gt;&gt;&gt; m.start(0)   # 3
&gt;&gt;&gt; m.end(0)     # 5
&gt;&gt;&gt; m.span(0)    # (3, 5)</code>

When a match succeeds, a Match object is returned, providing:

group([group1, …]) – returns the matched substring(s).

start([group]) – start index of the matched substring.

end([group]) – end index (exclusive) of the matched substring.

span([group]) – tuple of (start, end).

Another example:

<code>&gt;&gt;&gt;import re&gt;&gt;&gt; pattern = re.compile(r'([a-z]+) ([a-z]+)', re.I)
&gt;&gt;&gt; m = pattern.match('Hello World Wide Web')
&gt;&gt;&gt; print m
&lt;_sre.SRE_Match object at 0x10bea83e8&gt;
&gt;&gt;&gt; m.group(0)
'Hello World'
&gt;&gt;&gt; m.span(0)
(0, 11)
&gt;&gt;&gt; m.group(1)
'Hello'
&gt;&gt;&gt; m.span(1)
(0, 5)
&gt;&gt;&gt; m.group(2)
'World'
&gt;&gt;&gt; m.span(2)
(6, 11)
&gt;&gt;&gt; m.groups()
('Hello', 'World')
&gt;&gt;&gt; m.group(3)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
IndexError: no such group</code>

>> findall

Find all substrings matching the regex in a string and return them as a list; returns an empty list if no matches are found.

Note: match and search perform a single match, while findall finds all.

Syntax:

<code>re.findall(string[, pos[, endpos]])</code>

Parameters:

string – the target string.

pos (optional) – start position (default 0).

endpos (optional) – end position (default length of string).

Example – find all numbers:

<code>import re
pattern = re.compile(r'\d+')
result1 = pattern.findall('runoob 123 google 456')
result2 = pattern.findall('run88oob123google456', 0, 10)
print(result1)
print(result2)</code>

Output:

<code>['123', '456']['88', '12']</code>

>> re.finditer

Similar to findall , but returns an iterator yielding match objects.

<code>re.finditer(pattern, string, flags=0)</code>

Parameters:

Example:

<code>import re
it = re.finditer(r"\d+","12a32bc43jf3")
for match in it:
    print (match.group() )</code>

Output:

<code>12 32 43 3</code>

>> re.split

The split method splits a string by the occurrences of the pattern and returns a list.

Syntax:

<code>re.split(pattern, string[, maxsplit=0, flags=0])</code>

Parameters:

Example:

<code>&gt;&gt;&gt;import re&gt;&gt;&gt; re.split('\W+', 'runoob, runoob, runoob.')
['runoob', 'runoob', 'runoob', '']
&gt;&gt;&gt; re.split('(\W+)', ' runoob, runoob, runoob.')
['', ' ', 'runoob', ', ', 'runoob', ', ', 'runoob', '.', '']
&gt;&gt;&gt; re.split('\W+', ' runoob, runoob, runoob.', 1)
['', 'runoob, runoob, runoob.']
&gt;&gt;&gt; re.split('a*', 'hello world')
['hello world']</code>

>> Regular expression objects

re.compile() returns a RegexObject .

re.MatchObject provides group() to retrieve the matched string.

start() – returns the start position of the match.

end() – returns the end position of the match.

span() – returns a tuple (start, end).

>> Regular expression flags – optional modifiers

Regular expressions can include optional flag modifiers to control matching behavior. Multiple flags can be combined using bitwise OR (|).

For example, re.I | re.M sets both the I and M flags.

>> Regular expression patterns

Pattern strings use special syntax to represent a regular expression.

Letters and digits match themselves. Adding a backslash before them gives them special meaning.

Punctuation characters only match themselves when escaped; otherwise they have special meanings.

A backslash itself must be escaped. Because regular expressions often contain backslashes, it is best to use raw strings (e.g., r'\t' ).

>> Regular expression examples

Character matching

Character classes

Special character classes

- END -

pythonRegextext processingstring matchingre module
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.