re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.
Let's say we want to find all words that start with "P" in a string.
Python import re s = "Python is Powerful and Great" result = re.findall(r'\bP\w+', s) print(result)
Output['Python', 'Powerful']
Syntax of re.findall()
The syntax of re.findall() is simple:
import re
result = re.findall(pattern, string)
Parameters
- pattern: The regular expression pattern we are looking for.
- string: The string where we want to search for the pattern.
- result: This will be a list of all occurrences of the pattern in the string.
Return
The return type of re.findall() is always a list. This list contains all the non-overlapping matches of the pattern in the string. If no match is found, it will return an empty list.
Case Sensitivity in re.findall()
By default, re.findall() is case-sensitive. To make it case-insensitive, we can pass the re.IGNORECASE flag.
Python import re s = "Python is fun. python is cool." result = re.findall(r'python', s, re.IGNORECASE) print(result)
Output['Python', 'python']
Finding numbers using re.findall()
Here we use a pattern that matches any number. The regular expression \d+ looks for one or more digits in a row. So if the text contains numbers like "12 apples and 5 bananas", it will find "12" and "5" and give us those as the result
Python import re # Find all numbers in a string text = "There are 12 apples, 5 bananas, and 300 oranges." # \d+ matches one or more digits pattern = r'\d+' matches = re.findall(pattern, text) print(matches)
Matching dates using re.findall()
Here we use a pattern to find dates written in the format YYYY-MM-DD. The regular expression \d{4}-\d{2}-\d{2} is designed to find a four-digit year, followed by a two-digit month and a two-digit day. If the text contains "2024-12-25" and "2025-01-15" it will pick them out as matches.
Python import re #Find all dates in YYYY-MM-DD format text = "The event is on 2024-12-25, and the deadline is 2025-01-15." # Matches a date in YYYY-MM-DD format pattern = r'\d{4}-\d{2}-\d{2}' matches = re.findall(pattern, text) print(matches)
Output['2024-12-25', '2025-01-15']
Similar Reads
re.finditer() in Python re.finditer() method in Python is used to search for all matches of a pattern in a string and return them as an iterator. In this article, we will explore about re.finditer() method in Python.Let's understand this with the help of an example:Pythonimport re text = "GFG, O, B, GFG, O" pattern = "GFG"
2 min read
re.compile() in Python The re.compile() method in Python is used to compile a regular expression pattern into a regex object. Compiling a pattern makes it more efficient when we need to use the same pattern several times, as it avoids re-compiling the pattern each time. Letâs look at how re.compile() works and when we sho
3 min read
Python | Pandas Series.str.findall() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.findall() method is also used to find substrings or separators in each stri
2 min read
numpy.find() in Python numpy.core.defchararray.find(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An integer array wi
1 min read
re.match() in Python re.match method in Python is used to check if a given pattern matches the beginning of a string. Itâs like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using
2 min read