a re question

Christophe Delord christophe.delord at free.fr
Mon Sep 9 16:56:54 EDT 2002


match applies the regular expression at the beginning of the string. Try 'search' instead:

>>> 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
>>> print p.search(s)
<_sre.SRE_Match object at 0x400aed20>
>>> print p.search(s).groups()
('123456789   7',)


You can also use a different expression:

>>> p = re.compile(r'.*(\d{9,} {3,}\d)')
>>> print p.match(s)
<_sre.SRE_Match object at 0x400aed20>
>>> print p.match(s).groups()
('123456789   7',)




Christophe.


On Mon, 09 Sep 2002 16:10:39 -0400
Rajarshi Guha <rajarshi at presidency.com> wrote:

> 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
> 
> 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,


-- 

(o_   Christophe Delord                   _o)
//\   http://christophe.delord.free.fr/   /\\
V_/_  mailto:christophe.delord at free.fr   _\_V



More information about the Python-list mailing list