How to write simple code to match strings?

beginner zyzhu2000 at gmail.com
Wed Dec 30 00:01:05 EST 2009


Hi All,

I run into a problem.  I have a string s that can be a number of
possible things. I use a regular expression code like below to match
and parse it. But it looks very ugly. Also, the strings are literally
matched twice -- once for matching and once for extraction -- which
seems to be very slow. Is there any better way to handle this?


  def convert_data_item(s):
            if re.match('^\$?([-+]?[0-9,]*\.?[0-9,]+)$',s):
                g=re.match('^\$?([-+]?[0-9,]*\.?[0-9,]+)$',s)
                v=float(g.group(1).replace(',',''))
            elif re.match('^\(\$?([-+]?[0-9,]*\.?[0-9,]+)\)$',s):
                g=re.match('^\(\$?([-+]?[0-9,]*\.?[0-9,]+)\)$',s)
                v=-float(g.group(1).replace(',',''))
            elif re.match('^\d{1,2}-\w+-\d{1,2}$',s):
                v=dateutil.parser.parse(s, dayfirst=True)
            elif s=='-':
                v=None
            else:
                print "Unrecognized format %s" % s
                v=s
            return v

Thanks,
Geoffrey



More information about the Python-list mailing list