The re.findall
function in Python's re
module finds all occurrences of a pattern in a string and returns them as a list. This function is useful for extracting all matches of a pattern from a string.
Table of Contents
- Introduction
re.findall
Function Syntax- Examples
- Basic Usage
- Using Groups in Patterns
- Finding All Words in a String
- Using Flags with
re.findall
- Real-World Use Case
- Conclusion
Introduction
The re.findall
function in Python's re
module scans a string for all occurrences of a regular expression pattern and returns them as a list. This function is particularly useful when you need to find and extract multiple matches from a string.
re.findall Function Syntax
Here is how you use the re.findall
function:
import re
matches = re.findall(pattern, string, flags=0)
Parameters:
pattern
: The regular expression pattern to search for.string
: The string to search within.flags
: Optional. Flags that modify the behavior of the pattern, such asre.IGNORECASE
,re.MULTILINE
, etc.
Returns:
- A list of all non-overlapping matches in the string.
Examples
Basic Usage
Here is an example of how to use the re.findall
function to find all digit sequences in a string.
Example
import re
# Finding all digit sequences in a string
matches = re.findall(r'\d+', 'There are 123 apples and 45 bananas.')
print(matches)
Output:
['123', '45']
Using Groups in Patterns
This example demonstrates how to use groups in a regular expression pattern and access the matches.
Example
import re
# Finding all occurrences of a pattern with groups
matches = re.findall(r'(\d+)\s+apples', 'There are 123 apples and 45 bananas. And 678 apples.')
print(matches)
Output:
['123', '678']
Finding All Words in a String
This example demonstrates how to find all words in a string using re.findall
.
Example
import re
# Finding all words in a string
matches = re.findall(r'\b\w+\b', 'This is a test string.')
print(matches)
Output:
['This', 'is', 'a', 'test', 'string']
Using Flags with re.findall
This example demonstrates how to use flags with the re.findall
function to modify the behavior of the pattern.
Example
import re
# Finding all case-insensitive occurrences of a pattern
matches = re.findall(r'hello', 'Hello world! hello everyone.', re.IGNORECASE)
print(matches)
Output:
['Hello', 'hello']
Real-World Use Case
Extracting Email Addresses
In real-world applications, the re.findall
function can be used to extract all email addresses from a text.
Example
import re
def extract_emails(text):
pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
return re.findall(pattern, text)
# Example usage
text = 'Contact us at support@example.com or sales@example.com. You can also reach out to admin@domain.org.'
emails = extract_emails(text)
print(emails)
Output:
['support@example.com', 'sales@example.com', 'admin@domain.org']
Conclusion
The re.findall
function in Python's re
module finds all occurrences of a pattern in a string and returns them as a list. This function is useful for extracting all matches of a pattern from a string. Proper usage of this function can enhance the flexibility and power of your string processing tasks in Python.
Comments
Post a Comment
Leave Comment