[EMAIL PROTECTED] writes: > Hi, > I am new to python. I would like to know how to use python regular > expression to substitute string value? > I have an input string like this: > x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 > totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1 > > and I would like to convert it to: > rect x="11" y="0" width="760" height="14440" > > all I care about the input string is x, y, w, h.
I'd say you're better off with 'findall': - - import re line = "x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1" pattern = "x:(\d+) y:(\d+) w:(\d+) h:(\d+) area:(\d+)" x, y, width, height, area = re.findall(pattern, line)[0] print "rect x=\"%(x)s\" y=\"%(y)s\" width=\"%(width)s\" height=\"%(height)s\" ..." % locals() - - x .. area are strings when they come out of findall, so you may want to: - - x .. area = map (lambda x: int(x), re.findall (pattern, line)[0] - - -- Psi -- <http://www.iki.fi/pasi.savolainen> -- http://mail.python.org/mailman/listinfo/python-list