[Tutor] re.sub() query

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jun 20 12:20:14 CEST 2010


On 20/06/2010 10:54, Payal wrote:
> Hi,
> Is it possible to solve the below, w/o making a re object?
>
>>>> a
> 'Mary Had a Little Lamb'
>>>> p=re.compile('l',re.I)
>>>> re.sub(p,'-',a)
> 'Mary Had a -itt-e -amb'
>
> I cannot figure how to se re.I w/o involving  p.
>
> Thanks.
> With warm regards,
> -Payal

You can do this.

 >>> re.sub('[lL]','-',a)
'Mary Had a -itt-e -amb'

However it strikes me as overkill to use an re for something that could 
be done with the string replace function.

Kindest regards.

Mark Lawrence.



More information about the Tutor mailing list