I'm trying to get my head around some simple pattern matching or regular expressions using python.

Basically I want to be able to match "Failed 0.00/100.00", where 0.00 could be any decimal number.

I know using perl it would be something along the lines of "\d+\.\d+\/100\.0", but could anybody point me in the right direction?


I think that perl expression would not catch the Failed part, even I do not know perl.

I have not studied yet the python re -module for regular expressions.

I have however used basic string scanning in one function converting date strings. This can be adapted to your use also.

To test lets prove:

>>> this= '0.00/100.00'
>>> sep=[s for s in this if not s.isdigit()]
>>> sep
['.', '/', '.']

From this we can copy to make function to match required number/separator patern:

>>> a='line with also 234.090/2341.3248 dfalj alsdfj a afalfjd'
>>> def find_this(this):
	return [s for s in this if not s.isdigit()] == ['.', '/', '.']
>>> if filter(find_this,a.split()): print a

line with also 234.090/2341.3248 dfalj alsdfj a afalfjd
>>> filter(find_this,alist)
>>> b="This line has 100.00 and has also 1/3=0.333 but not in required pattern."
>>> if filter(find_this,b.split()): print b
##prints nothing

Something like this would work if you knew what cam right before the number you need. I use re.findall all the time when scanning html.

import re
s = 'this is a string containg stuff like 10.255/100.0 and the likes."
found = re.findall('like (.*)/100.0')

#Some crude error checking
if "." in found[0]:
    print float(found[0])
    print int(found[0])

This will match,0.00 can be any decimal number.

'Failed \d.{3,3}/\d.{3,3}.\d.{0,0}'
import re
txt = "Failed 9.45/100.00"

if re.match(r'Failed \d.{3,3}/\d.{3,3}.\d.{0,0}', txt):
    print 'Match'
    print 'Match attempt failed'
import re

text = '''\
A fast and friendly dog.
My car is "red."
"Failed 9.99/100.00"
"Hei, this is a \"string\" with data!"
"0.99 100.0 Failed"

comp = re.compile(r'Failed \d.{3,3}/\d.{3,3}.\d.{0,0}')
test_match = comp.findall(text)
print 'Match is %s' % (test_match)  # Match is ['Failed 9.99/100.00']

Thanks a ton guys. Your examples have really helped, I'll see what I'm able to do with them!