Google News
logo
Python Program to Using the findall() function
The `findall()` function is used to find all occurrences of a pattern in a string and returns a list of all matched substrings. The syntax for `findall()` is as follows :
re.findall(pattern, string, flags=0)​
where `pattern` is the regular expression pattern to be searched, `string` is the input string to be searched, and `flags` is an optional argument that can be used to modify the search behavior.

Here's an example program that uses the `findall()` function to find all occurrences of a pattern in a string :
Program :
import re

# input string
string = "The quick brown fox jumps over the lazy dog in Spain"

# regular expression pattern
pattern = r"The.*Spain"

# search for all occurrences of the pattern in the string
matches = re.findall(pattern, string)

# print the matched substrings
print(matches)
Output :
['The quick brown fox jumps over the lazy dog in Spain']
In this example, the regular expression pattern `r"The.*Spain"` matches any substring that starts with "The" and ends with "Spain".

The `findall()` function is used to find all occurrences of this pattern in the input string. Since there is only one match in the string, the function returns a list containing a single string.