I have a weird case where this simple code is not functioning as expected:
import re
text = 'This Level: 75.3'
matches = re.search(r'(?:(?:\d{1,3},)(?:\d{3},)*(?:\d{3})|\d*)(?:\.\d+)?', text)
print(matches.match)
I keep getting a blank string returned... however, I would expect this to be 75.3
. This works for other use cases, such as:
assert util.strip_str_to_float('7') == 7.0
assert util.strip_str_to_float('75') == 75.0
assert util.strip_str_to_float('75.5') == 75.5
assert util.strip_str_to_float('7.7.9') == 7.7
assert util.strip_str_to_float('1,298.3 Gold') == 1298.3
Ultimately, I'm trying to pull out and convert the first float from a given string... I wasn't expecting this test case to be a failure. It seems to be failing specifically when the matching does not start at the beginning of the string. The search seems to work fine if I remove the non-capturing groups, for example, this works:
matches = re.search(r'\d*\.\d+', text)
But this does not:
matches = re.search(r'\d*(?:\.\d+)?', text)
Any ideas...?