Regural expression puzzle for gurus

John Machin machin_john_888 at hotmail.com
Fri Jul 13 14:56:41 CEST 2001


Joonas Paalasmaa <joonas.paalasmaa at nokia.com> wrote in message news:<3B4EA470.AA7B0D12 at nokia.com>...
> Pekka Niiranen wrote:
> > 
> > I have a string in the middle of a text starting with #-sign and ending
> > to \W.
> > I can find it with re.compile('#\w+') and  would like to replace it by
> > adding a
> > # -sign also to the end of it.
> > 
> > Example:
> > 
> >             original string:           xx:yy:#AAA.!:-#BBB:2324:#CCC:!"¤%
> > 
> >             after replacement:    xx:yy:#AAA#.!:-#BBB#:2324:#CCC#:!"¤%
> > 
> > How can I do replacement with a single regural expression line ?
> > 
> > I have found a solution like:
> >            text = 'xx:yy:#AAA:-#BBB:aa'
> >            line = re.compile('#\w+')
> >            list = line.findall(text)
> >            for i in range(len(list)):
> >                  text = text.replace(list[i],list[i]+'#')
> > 
> > >>>  text
> > 'xx:yy:#AAA#:-#BBB#:aa'
> > 
> > I would like to do this without the for -loop (unless somebody can
> > convince
> > me the found solution is the fastest available)
> 
> Try this.
> 
> import re
> text = """xx:yy:#AAA.!:-#BBB:2324:#CCC:!"¤%"""
> print re.sub(r"(#\w+)(\W)", r"\1#\2", text)
> 
> - Joonas

The \W is not only unnecessary but will cause failure if the input
string ends in the desired pattern -- there would be nothing for \W to
match. Quite simply, all you need is:

re.sub(r"(#\w+)", r"\1#", text)



More information about the Python-list mailing list