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 a UUID using Python regex?
- How to replace all using Python regex?
- How to match a year with Python Regex?
- How to match whitespace in Python regex?
- How to remove numbers from a string using Python regex?
- How to match an IP address with regex in Python?
- How to get all matches from a regex in Python?
- How to match the beginning of a line with Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to use word boundaries in Python Regex?
See more codes...