This is one of those things where I\'m sure I\'m missing something simple, but... In the sample program below, I\'m trying to use Python\'s RE library to parse the string \"
You should use re.findall
instead:
>>> line = ' 0 repaired, 90.31% done'
>>>
>>> pattern = re.compile("\d+[.]\d+(?=%)")
>>> re.findall(pattern, line)
['90.31']
re.match
will match at the start of the string. So you would need to build the regex for complete string.
try this if you really want to use match:
re.match(r'.*(\d+\.\d+)% done$', line)
r'...' is a "raw" string ignoring some escape sequences, which is a good practice to use with regexp in python. – kratenko (see comment below)
match
only matches from the beginning of the string. Your code works fine if you do pct_re.search(line)
instead.