Python regular expression not matching

Solution 1:

match only matches from the beginning of the string. Your code works fine if you do pct_re.search(line) instead.

Solution 2:

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.

Solution 3:

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)