error in module re?

Siggy Brentrup bsb at baal.infodrom.north.de
Mon Sep 13 19:17:49 EDT 1999


sca at isogmbh.de (Chris...) writes:

> Dear Pythoners...
> 
>   Is this an implementation or documentation error?
> >>> import re
> >>> re.sub('(a+)\s(b+)', '\2-\1', 'aaa bbb')
> '\002-\001'
> instead of
> 'bbb-aaa'
> The documentation says, that you can use backreferences in replacement
> string, but only \g<number> works.

Try duplicating the backslashes or use a "raw" string constant:

Python 1.5.1 (#1, Dec 17 1998, 20:58:15)  [GCC 2.7.2.3] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import re
>>> re.sub('(a+)\s(b+)', '\2-\1', 'aaa bbb')
'\002-\001'
>>> re.sub('(a+)\s(b+)', '\\2-\\1', 'aaa bbb')
'bbb-aaa'
>>> re.sub('(a+)\s(b+)', r'\2-\1', 'aaa bbb')
'bbb-aaa'
>>> 


CU
  Sigg

-- 
nothing exiting yet:

Siggy Brentrup - bsb at baal.infodrom.north.de - voice: +49-441-6990134





More information about the Python-list mailing list