Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression not matching

Tags:

python

regex

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 "line" to get the floating-point number just before the percent sign, i.e. "90.31". But the code always prints "no match".

I've tried a couple other regular expressions as well, all with the same result. What am I missing?

#!/usr/bin/python
import re
line = '    0 repaired, 90.31% done'
pct_re = re.compile(' (\d+\.\d+)% done$')
#pct_re = re.compile(', (.+)% done$')
#pct_re = re.compile(' (\d+.*)% done$')
match = pct_re.match(line)
if match: print 'got match, pct=' + match.group(1)
else: print 'no match'
like image 660
Matt Avatar asked Jul 16 '13 15:07

Matt


People also ask

Does Python not match?

Not Equal Operator in Python If the values compared are not equal, then a value of false is returned. != is the symbol we use for the not equal operator.

How do you negate a regex in Python?

Practical Data Science using Python An regex of '[^abdfgh]' will match any single character which is NOT one of 'a', 'b', 'd', 'f', 'g' or 'h'. This is a negated character class, and is indicated by the '^' character at the start of the character class.

Why * is used in regex?

* - means "0 or more instances of the preceding regex token"


2 Answers

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

like image 131
Daniel Roseman Avatar answered Oct 21 '22 18:10

Daniel Roseman


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.

like image 34
Rohit Jain Avatar answered Oct 21 '22 17:10

Rohit Jain