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'
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.
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.
* - means "0 or more instances of the preceding regex token"
match
only matches from the beginning of the string. Your code works fine if you do pct_re.search(line)
instead.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With