Raw string substitution problem

Ed Keith e_d_k at yahoo.com
Wed Dec 16 15:53:26 EST 2009


--- On Wed, 12/16/09, Peter Otten <__peter__ at web.de> wrote:

> Another possibility:
> 
> >>> print re.sub('abc', lambda m: r'a\nb\n.c\a',
> '123abcdefg')
> 123a\nb\n.c\adefg

I'm not sure whether that is clever, ugly, or just plain strange! 

I think I'll stick with:

>>> m = re.match('^(.*)abc(.*)$', '123abcdefg')
>>> print m.group(1) + r'a\nb\n.c\a' + m.group(2)
123a\nb\n.c\adefg

It's much less likely to fry the poor maintenance programmer's mind.

    -EdK

Ed Keith
e_d_k at yahoo.com

Blog: edkeith.blogspot.com



      



More information about the Python-list mailing list