Regular expressions question

Vlastimil Brom vlastimil.brom at gmail.com
Thu Oct 2 17:54:19 EDT 2008


2008/10/2 aditya shukla <adityashukla1983 at gmail.com>

> Hello folks ,
>
> I trying to match a pattern in a string , i am new in using re .This is
> what is happening
>
> When i do this
>
> p = re.compile('(\[&&NHX:)')
> >>> m = p.match("[&&NHX:C=0.195.0]")
> >>> print m
> <_sre.SRE_Match object at 0x013FE1E0>
>  --- thus i am able to find the match
> but when i use the string
>
> m = p.match("-bin-ulockmgr_server:0.99[&&NHX:")
> >>> print m
> None
> -i am not able to find the match .
>
> Can someone help me here.
>
> Thanks
>
> Aditya
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
> Hi, You may want to check the re documantation - chapter Matching vs
Searching

http://docs.python.org/library/re.html#matching-vs-searching

"... match checks for a match only at the beginning of the string ..."

>>> import re
>>> p = re.compile('(\[&&NHX:)')
>>> p.match("[&&NHX:C=0.195.0]")
<_sre.SRE_Match object at 0x01C2FEA0>
>>> p.match("-bin-ulockmgr_server:0.99[&&NHX:")
>>> p.search("-bin-ulockmgr_server:0.99[&&NHX:")
<_sre.SRE_Match object at 0x01C2FF60>
>>> p.findall("-bin-ulockmgr_server:0.99[&&NHX:")
['[&&NHX:']
>>>

hth

 vbr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081002/082932aa/attachment-0001.html>


More information about the Python-list mailing list