a re question

sismex01 at hebmex.com sismex01 at hebmex.com
Mon Sep 9 16:26:50 EDT 2002


> 
> Hi,
>   I have a file with lines of the format:
> 
> 001 Abc D Efg 123456789   7 100 09/05/2002 20:23:23
> 001 Xya FGh   143557789   7 100 09/05/2002 20:23:23
> 


Are the fields are always fixed length?


> I am trying to extract the 9 digit field and the single digit field
> immediatley after that.
> 
> When I use Visual Regexp to try out the regexp 
> 
> (\d{9,} {3,}\d)
> 
> it highlights the 2 fields exactly. 
> 
> But when I use the following Python code I get None:
> 
> >> s='001 Abc D Efg 123456789   7 100 09/05/2002 20:23:23'
> >> p = re.compile(r'(\d{9,} {3,}\d)')
> >> print p.match(s)
> >> None
> 
> Could anybody point out where I'm going wrong?
> 
> Thanks,
> 

You could be a bit more explicit with your regexp.
Using the input you gave:

> 001 Abc D Efg 123456789   7 100 09/05/2002 20:23:23
> 001 Xya FGh   143557789   7 100 09/05/2002 20:23:23

I'd think something like this is better:

rx = re.compile(r"\d+\s\D{9}\s(\d{9})\s+(\d+)")

And that gives us:

>>> import re
>>> rx = re.compile(r"(\d{9})\s+(\d+)")
>>> s = """
... 001 Abc D Efg 123456789   7 100 09/05/2002 20:23:23
... 001 Xya FGh   143557789   7 100 09/05/2002 20:23:23
... """
>>> rx.findall(s)
[('123456789', '7'), ('143557789', '7')]
>>> 

Something like that.

-gus
--

Advertencia: 
La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.


Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.




More information about the Python-list mailing list