Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to write a 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 in searching 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 supports metacharacters for pattern matching.
There are various ways to use the re.findall() method in Python, such as ?
Syntax
re.findall(pattern, string, flags=0)
Parameters:
- pattern ? The regular expression pattern to search for
- string ? The input string to search in
- flags ? Optional flags to modify pattern behavior
Find One or More Digits
The following example uses r"\d+" to find one or more digits, as '\d' represents digits. The re.findall() method will extract all matching parts and return 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)
The output of the above code is ?
['123', '456']
Find All Words in a String
In this example, we use the r"\w+" pattern to find all the words present in the given string. The regex \w+ matches one or more word characters (letters, digits, underscores) ?
# Import regular expressions module import re # Define a string to search text = "Hello world, welcome to Python!" # Use a regular expression to find all words in the string pattern = r"\w+" # Find all matches of the pattern in the string matches = re.findall(pattern, text) # Print the result print(matches)
The output of the above code is ?
['Hello', 'world', 'welcome', 'to', 'Python']
Extract Email Addresses
In the following example, we use \b[\w.%+-]+@[\w.-]+\.\w+\b regex pattern to capture all email addresses. 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 text = "Email me at example@tutorialspoint.com or contact@tutorix.com" # Define a pattern to match email addresses pattern = r"\b[\w.%+-]+@[\w.-]+\.\w+\b" # Use re.findall to find all occurrences of the pattern in the string matches = re.findall(pattern, text) # Print the result print(matches)
The output of the above code is ?
['example@tutorialspoint.com', 'contact@tutorix.com']
Find Dates in a Text
Here, our pattern to find dates is \b\d{2,4}[-/]\d{2}[-/]\d{2,4}\b. This program will find dates written with different separators like - or / ?
# Import regular expressions module
import re
# Define a string with dates to search
text = "My important dates are: 26-05-2024, 27/06/2025, 2023-07-28"
# Define a pattern to match dates in various formats
pattern = r"\b\d{2,4}[-/]\d{2}[-/]\d{2,4}\b"
# Find all matches of the pattern in the text
matches = re.findall(pattern, text)
# Print the results
print(matches)
The output of the above code is ?
['26-05-2024', '27/06/2025', '2023-07-28']
Common Regex Patterns
| Pattern | Description | Example |
|---|---|---|
\d+ |
One or more digits | 123, 4567 |
\w+ |
One or more word characters | hello, Python123 |
[a-zA-Z]+ |
One or more letters | abc, XYZ |
\S+ |
One or more non-whitespace | hello!, @#$ |
Conclusion
The re.findall() method is powerful for extracting patterns from text using regular expressions. Use specific patterns like \d+ for digits, \w+ for words, and complex patterns for emails or dates to extract structured data efficiently.
