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 URL using Python regex?
- How to use word boundaries in Python Regex?
- How to remove special characters using Python regex?
- How to replace all using Python regex?
- How to match a plus sign in Python regex?
- How to match HTML tags with regex in Python?
- How to match zero or one occurence in Python regex?
- How to match a year with Python Regex?
- How to split using Python regex?
- How to replace in a file using Python regex?
See more codes...