regex help

Peter Otten __peter__ at web.de
Tue May 16 11:44:06 EDT 2006


Lance Hoffmeyer wrote:

> I have the following table and I am trying to match percentage the 2nd
> column on the 2nd Tiger line (9.0).
> 
> I have tried both of the following.  I expected both to match but neither
> did?  Is there a modifier
> I am missing?  What changes do I need to make these match?  I need to keep
> the structure of the regex the same.
> 
> TIGER.append(re.search("TIGER\s{10}.*?(?:(\d{1,3}\.\d)\s+){2}",
> target_table).group(1))
> TIGER.append(re.search("^TIGER.*?(?:(\d{1,3}\.\d)\s+){2}",
> target_table).group(1))

You can try the re.DOTALL flag (prepend the regex string with "(?s)"), but
I'd go with something really simple:

instream = iter(target_table.splitlines()) # or: instream = open(datafile)
for line in instream:
    if line.startswith("TIGER"):
        value = instream.next().split()[1] # or ...[0]? they are both '9.0'
        TIGER.append(value)
        break

Peter




More information about the Python-list mailing list