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 thematchesvariableprint(matches): prints the matches found
Helpful links
More of Python Regex
- How to match a YYYY-MM-DD date with Python Regex?
- How to match a year with Python Regex?
- How to use word boundaries in Python Regex?
- How to match a URL using Python regex?
- How to match whitespace in Python regex?
- How to match a UUID using Python regex?
- How to get a group from a regex in Python?
- How to make a case insensitive match with Python regex?
- How to match any symbol except a given one with Python regex?
- How to replace a certain group using Python regex?
See more codes...