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 replace a certain group using Python regex?
- How to match a UUID using Python regex?
- How to match whitespace in Python regex?
- How to replace in a file using Python regex?
- How to perform a zero length match with Python Regex?
- How to replace all using Python regex?
- How to match a hex number with regex in Python?
- How to match a plus sign in Python regex?
- How to match a float with regex in Python?
- How to regex match excluding a character in Python?
See more codes...