python-regexHow to find all matches with regex in Python?
To find all matches with regex in Python, you can use the re.findall()
function. This function takes two arguments: the pattern to search for, and the string to search in.
Example code
import re
string = "The quick brown fox jumps over the lazy dog"
pattern = "The.*dog"
matches = re.findall(pattern, string)
print(matches)
Output example
['The quick brown fox jumps over the lazy dog']
Code explanation
import re
: imports there
module, which contains thefindall()
function.string
: the string to search in.pattern
: the pattern to search for.re.findall(pattern, string)
: searches for all matches of the pattern in the string.print(matches)
: prints the matches.
Helpful links
More of Python Regex
- How to match the beginning of a line with Python regex?
- How to match a year with Python Regex?
- How to replace all using Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to use word boundaries in Python Regex?
- How to remove numbers from a string using Python regex?
- How to split using Python regex?
- How to match certain amount of digits with Python regex?
- How to perform a zero length match with Python Regex?
- How to validate an IP using Python regex?
See more codes...