🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.findallFunction 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment