RE module question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Oct 3 05:01:28 EDT 2008


En Fri, 03 Oct 2008 03:59:18 -0300, aditya shukla  
<adityashukla1983 at gmail.com> escribió:

> Hello folks :
>
> i have a string "-bin-ulockmgr_server:0.99[&&NHX:C=0.195.0]" from which i
> want to delete [&&NHX:C=0.195.0] .
> I wrote a regular expression for [&&NHX:C=0.195.0] as
> \[&&(\w)+:\w=((-)?(\d|\d\d|\d\d\d)\.)+\d\]
>
> now when i do
>
> p = re.compile('\[&&(\w)+:\w=((-)?(\d|\d\d|\d\d\d)\.)+\d\]')
> m =p.search("-bin-ulockmgr_server:0.99[&&NHX:C=0.195.0]")
> print m
> <_sre.SRE_Match object at 0x01314EE8>
>>>> print m.group()
> [&&NHX:C=0.195.0]
>
> So i guess i am able to find the substring , my question is how can i  
> delete
> this substring from the original string? ie
> -bin-ulockmgr_server:0.99 should be my output.

Instead of p.search, use p.sub
Or, if you need the match for other purposes, delete the characters from  
m.start() to m.end()

See http://docs.python.org/library/re.html#regular-expression-objects

-- 
Gabriel Genellina




More information about the Python-list mailing list