I have a Python string containing information that I want to pull out using regex.
Example:
"The weather is 75 degrees with a humidity of 13%"
I want to just pull out the “75” and the “13.” Here is what I’ve tried so far in Python.
import re
str = "The weather is 75 degrees with a humidity of 13%"
m = re.search("The weather is \d+ degrees with a humidity of \d+%", str)
matched = m.group()
However, this obviously matches the entire string instead of just the parts I want. How do I pull out just the numbers that I want? I’ve looked into backreferences but it seems to only apply to within the regex pattern itself.
you need to wrap what you want in parenthesis …
or just use
findallto just get the numbers out of any string