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 URL path using Python regex?
- How to replace all using Python regex?
- How to replace a certain group using Python regex?
- How to match a hex number with regex in Python?
- How to match a year with Python Regex?
- How to quote in Python regex?
- How to match one or more occurence in Python regex?
- How to match an IP address with regex in Python?
- How to match a phone number in Python regex?
- How to get all matches from a regex in Python?
See more codes...