The re (Regular Expressions) Module
On this page
RegEx in PythonRegEx Functions and Syntax OverviewRegEx FunctionsMetacharactersFlagsSpecial SequencesSetsApplying RegEx FunctionsUsingfindall() FunctionUsing search() FunctionUsing split() FunctionThe re module lets you search, match, and manipulate text patterns efficiently using regular expressions. They are powerful for validating input, cleaning data, and analyzing text.
RegEx in Python
Before using any regular expressions, you must import Python’s built-in re module:
import reWhen you have imported the re module, you can start using regular expressions.
A regular expression describes a text pattern. In the pattern below, ^ marks the start, .* permits zero or more characters other than a newline, and $ marks the end (or the position before a final newline). re.search() returns a match object when it finds the pattern, or None otherwise.
Let’s search for text that starts with “The” and ends with “Spain”:
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
if x:
print("YES! We have a match!")
else:
print("No match")Output
YES! We have a match!
RegEx Functions and Syntax Overview
Before diving into examples, let’s review the key functions and symbols that make regular expressions so powerful in Python.
Regular expressions (RegEx) work through the re module, which provides several built-in functions for searching, splitting, and replacing text.
RegEx Functions
The re module offers a set of functions that allow you to search, extract, or modify text patterns:
| Function | Description |
|---|---|
findall() |
Returns a list containing all matches |
search() |
Returns a Match object if a match is found anywhere in the string |
split() |
Splits a string into a list at each match |
sub() |
Replaces one or many matches with a specified string |
These functions form the foundation of most regular expression tasks in Python.
Metacharacters
Metacharacters are special symbols with unique meanings in regular expressions. They let you define flexible patterns instead of exact text matches.
| Character | Description | Example |
|---|---|---|
[] |
A set of characters | [a-m] |
\ |
Signals a special sequence (can also escape special characters) | \d |
. |
Any character except newline | he..o |
^ |
Matches the start of a string | ^hello |
$ |
Matches the end of a string | planet$ |
* |
Zero or more occurrences | he.*o |
+ |
One or more occurrences | he.+o |
? |
Zero or one occurrence | he.?o |
{} |
Exactly the specified number of occurrences | he.{2}o |
| |
Either/or condition | falls|stays |
() |
Capture and group expressions | (abc) |
Flags
Flags modify how regular expressions are interpreted. They can be combined with functions like re.search() or re.findall() to change matching behavior.
| Flag | Shorthand | Description |
|---|---|---|
re.ASCII |
re.A |
Use ASCII rules for character classes such as \w, \d and \s |
re.DEBUG |
– | Print debugging information |
re.DOTALL |
re.S |
Make . match all characters, including newlines |
re.IGNORECASE |
re.I |
Case-insensitive matching |
re.MULTILINE |
re.M |
Make ^ and $ match at line boundaries |
re.UNICODE |
re.U |
Default behavior (matches Unicode text) |
re.VERBOSE |
re.X |
Allow spaces and comments in complex patterns for readability |
Special Sequences
Special sequences start with a backslash (\) and represent common text patterns like digits, spaces, or word characters. When used inside raw strings (like r"\d"), Python reads them literally.
| Sequence | Description | Example |
|---|---|---|
\A |
Match if characters are at the start of the string | \AThe |
\b |
Match at the start or end of a word | r"\bain" or r"ain\b" |
\B |
Match a position that is not a word boundary | r"\Bain" |
\d |
Match any Unicode decimal digit; use [0-9] for ASCII digits |
\d |
\D |
Match any non-digit | \D |
\s |
Match any whitespace (space, tab, newline) | \s |
\S |
Match any non-whitespace character | \S |
\w |
Match any word character (letters, digits, underscore) | \w |
\W |
Match any non-word character | \W |
\Z |
Match if characters are at the end of the string | Spain\Z |
Sets
A set is a collection of characters enclosed in square brackets [ ]. It matches any single character inside the brackets.
| Set | Description |
|---|---|
[arn] |
Matches if one of the characters a,r, or n is present |
[a-n] |
Matches any lowercase letter between a and n |
[^arn] |
Matches any character excepta,r, and n |
[0123] |
Matches if one of 0,1,2, or 3 is present |
[0-9] |
Matches any digit between 0 and 9 |
[0-5][0-9] |
Matches any two-digit number from 00 to 59 |
[a-zA-Z] |
Matches any alphabetic character, lowercase or uppercase |
[+] |
Matches a literal + (special characters lose meaning inside sets) |
With these tools — functions, metacharacters, flags, special sequences, and sets — you can define almost any pattern imaginable, from finding numbers in a text to validating phone numbers or emails.
Applying RegEx Functions
We will now demonstrate how useful functions such as findall(),search(),split(), and sub() can be used with re.
Using findall() Function
The findall() function returns a list of all parts of the string that match a pattern.
import re
text = "I have 2 apples, 5 bananas, and 12 oranges."
numbers = re.findall(r"\d+", text)
print(numbers)Output
['2', '5', '12']
\d+means “find one or more digits.”- The function goes through the text and extracts every number it finds.
- The result is a list of numbers written as strings.
- Note: The prefix
r"..."tells Python to treat the pattern as a raw string, meaning backslashes (\) are read literally and not as escape characters.
Using search() Function
The search() function looks for the first match of a pattern and returns a match object if found.
text = "Hello world"
match = re.search(r"world", text)
if match:
print("Found:", match.group())
else:
print("No match")Output
Found: world
Explanation:
- The pattern
"world"is found inside the string"Hello world". match.group()returns the exact part of the text that matched.
Using split() Function
The split() function breaks a string into parts wherever the pattern appears.
text = "apple,banana;cherry orange"
result = re.split(r"[,; ]", text)
print(result)Output
['apple', 'banana', 'cherry', 'orange']
Explanation:
- The pattern
[,; ]tells Python to split wherever there is a comma, semicolon, or space. - The result is a clean list of fruit names.
This handles simple delimiter-separated text. For CSV with quoted fields, use the csv module introduced in File Handling.