Please help with regular expression finding multiple floats

Jeremy jlconlin at gmail.com
Fri Oct 23 00:26:01 CEST 2009


I have text that looks like the following (but all in one string with
'\n' separating the lines):

    1.0000E-08   1.58024E-06 0.0048
    1.0000E-07   2.98403E-05 0.0018
    1.0000E-06   8.85470E-06 0.0026
    1.0000E-05   6.08120E-06 0.0032
    1.0000E-03   1.61817E-05 0.0022
    1.0000E+00   8.34460E-05 0.0014
    2.0000E+00   2.31616E-05 0.0017
    5.0000E+00   2.42717E-05 0.0017
      total      1.93417E-04 0.0012

I want to capture the two or three floating point numbers in each line
and store them in a tuple.  I want to find all such tuples such that I
have
[('1.0000E-08', '1.58024E-06', '0.0048'),
 ('1.0000E-07', '2.98403E-05', '0.0018'),
 ('1.0000E-06', '8.85470E-06', '0.0026'),
 ('1.0000E-05', '6.08120E-06', '0.0032'),
 ('1.0000E-03', '1.61817E-05', '0.0022'),
 ('1.0000E+00', '8.34460E-05', '0.0014'),
 ('2.0000E+00', '2.31616E-05', '0.0017'),
 ('5.0000E+00', '2.42717E-05', '0.0017')
 ('1.93417E-04', '0.0012')]

as a result.  I have the regular expression pattern

fp1 = '([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s+'

which can find a floating point number followed by some space.  I can
find three floats with

found = re.findall('%s%s%s' %fp1, text)

My question is, how can I use regular expressions to find two OR three
or even an arbitrary number of floats without repeating %s?  Is this
possible?

Thanks,
Jeremy







More information about the Python-list mailing list