problem with newlines in regexp substitution

Florian Schulze florian.proff.schulze at gmx.net
Thu Feb 23 12:15:22 EST 2006


See the following results:

Python 2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> s = "1"
>>> re.sub('1','\\n',s)
'\n'
>>> '\\n'
'\\n'
>>> re.sub('1',r'\\n',s)
'\\n'
>>> s.replace('1','\\n')
'\\n'
>>> repl = '\\n'
>>> re.sub('1',repl,s)
'\n'
>>> s.replace('1',repl)
'\\n'

Why is the behaviour of the regexp substitution so weird and can I prevent 
that? It breaks my asumptions and thus my code.

Regards,
Florian Schulze




More information about the Python-list mailing list