python-regexHow to extract found values with Python regex?
Python regex can be used to extract found values from a string. The re.findall()
function can be used to find all matches in a string and return them as a list.
Example code
import re
string = "The cat in the hat"
matches = re.findall("cat", string)
print(matches)
Output example
['cat']
Code explanation
import re
: imports the Python regex modulestring = "The cat in the hat"
: assigns the string to be searchedmatches = re.findall("cat", string)
: uses there.findall()
function to search for the pattern "cat" in the string and assign the matches to thematches
variableprint(matches)
: prints the matches found
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...