Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Write Python Regular Expression to Use re.findall



This article will explain how to write a Python regular expression to use re.findall() method. Regular expression, or regex, is used insearching and extracting patterns in text. Python provides the re module to work with regex, and re.findall() to find all matches of a pattern in a string.

The re.findall() method returns a list of all occurrences of a pattern. It works with strings for extracting data. And it supports metacharacters for pattern matching.

There are various ways to use the re.findall() method in Python, such as -

Example: Finds One or More Digits

The following example will use r"\d+" to find one or more digits, as '\d' represents digits. And re.findall() method will extract all matching parts. So the output will be a list of found numbers.

# Import regular expressions module
import re

# Define a string to search
text = "abc 123 xyz 456"

# Use a regular expression 
pattern = r"\d+"

# Find all matches of the pattern in the text
matches = re.findall(pattern, text)

# Print the matches found
print(matches)  

Output

This will create the following outcome -

['123', '456']

Example: Find all words in a string

This is another easy way to use the re.findall() function. In this example, we will use the r"\w+" pattern to find all the words present in the given string. The regex \w+ is used to find all words by matching letter sequences.

# Import regular expressions module
import re

# Define a string to search
txt = "Hello world, welcome to Python!"

# Use a regular expression to find all words in the string
pat = r"\w+"

# Find all matches of the pattern in the string
res = re.findall(pat, txt)

# Print the result
print(res)

Output

This will generate the following result -

['Hello', 'world', 'welcome', 'to', 'Python']

Example: Extract Email Addresses

In the following example, we are going to use \b[\w.%+-]+@[\w.-]+\.\w+\b regex pattern to capture all email addresses. And the re.findall() method will search and extract email patterns from the given text.

# Import regular expressions module
import re

# Define a string to search
txt = "Email me at example@tutorialspoint.com or contact@tutorix.com"

# Define a pattern to match email addresses
pat = r"\b[\w.%+-]+@[\w.-]+\.\w+\b"

# Use re.findall to find all occurrences of the pattern in the string
res = re.findall(pat, txt)

# Print the result
print(res) 

Output

This will produce the following result -

['example@tutorialspoint.com', 'contact@tutorix.com']

Example: Find Dates in a Text

Here, our pattern to find dates in the given date is \b\d{2,4}[-/]\d{2}[-/]\d{2,4}\b. So our program will find dates written with different separators like - or /.

# Import regular expressions module
import re

# Define a string with dates to search
txt = "My important dates are: 26-05-2024, 27/06/2025, 2023-07-28"

# Define a pattern to match dates in various formats
pat = r"\b\d{2,4}[-/]\d{2}[-/]\d{2,4}\b"

# Find all matches of the pattern in the text
res = re.findall(pat, txt)

# Print the results
print(res) 

Output

This will lead to the following outcome -

['26-05-2024', '27/06/2025', '2023-07-28']
Updated on: 2025-05-26T17:51:57+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements