Help: re.match puzzle

Anton Muhin antonmuhin at sendmail.ru
Tue Mar 18 15:58:34 EST 2003


Vivek Sawant wrote:
> I am not able to figure out why re.match is not matching the following 
> simple regex.
> 
> Python 2.2.2 (#1, Nov 27 2002, 11:14:26)
> [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import string
>  >>> import re
>  >>> line = '24 bytes from 128.59.67.201: icmp_seq=1 ttl=56 time=15.2 ms'
>  >>> pat = '(?P<time>time=)'
>  >>> line
> '24 bytes from 128.59.67.201: icmp_seq=1 ttl=56 time=15.2 ms'
>  >>> pat
> '(?P<time>time=)'
>  >>> m = re.match (pat, line)
>  >>> print m
> None
>  >>>
> 
> Any ideas?
> Thanks.
> 
> \vivek
> 

Because match matches from the *begining* of the string. You need search:

 >>> import re
 >>> m = re.search('(?P<time>time=)', '24 bytes from 128.59.67.201: 
icmp_seq=1 ttl=56 time=15.2 ms')
 >>> m.groups('time')
('time=',)





More information about the Python-list mailing list