BenF97 BenF97 - 24 days ago 6
Python Question

Regex to get number after matched pattern

I have created a regex pattern to match the following:

'x': 10.61 # Any number, can be negative


Regex:
'x':\s[-+]?\d*\.*\d+


However, I would like to get only the number, and so have attempted the following:

re.findall("'x':\K\s[-+]?\d*\.*\d+", info)


But from what I understand
\K
doesn't work on python.

Is there any alternative that would match e.g:
10.61
only?

Answer

This should work:

info = "'x': 10.61"
items = re.findall("'x':\s([-+]?\d*\.*\d+)", info)
print(items[0]) # 10.61