Please help with regular expression finding multiple floats
rhodri at wildebst.demon.co.uk
Fri Oct 23 00:54:18 CEST 2009
On Thu, 22 Oct 2009 23:26:01 +0100, Jeremy <jlconlin at gmail.com> wrote:
> 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
> 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
> [('1.0000E-08', '1.58024E-06', '0.0048'),
> ('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.
Hmm. Is ".01" really valid? Oh well, let's assume so. I'd
seriously recommend using a raw string r'....' to define fp1
with though; it's a good habit to get into with regular expressions,
and when (not if) the fact that none of your backslashes are
escaped matters, you won't waste hours wondering what just bit you.
> 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
Yes. On the off-chance that this is homework, I'll just observe that
the only difference between detecting repeated digits (say) and repeated
float-expressions is exactly what you apply the repetition operators to.
The documentation for the 're' module at python.org is your friend!
Rhodri James *-* Wildebeest Herder to the Masses
More information about the Python-list