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 whitespace in Python regex?
- How to use word boundaries in Python Regex?
- How to match a question mark in Python regex?
- How to get a group from a regex in Python?
- How to match a plus sign in Python regex?
- How to match a UUID using Python regex?
- How to remove special characters using Python regex?
- How to match an IP address with regex in Python?
- How to use named groups with regex in Python?
- How to replace a certain group using Python regex?
See more codes...