[issue4185] re module treats raw strings as normal strings

Ezio Melotti report at bugs.python.org
Thu Oct 23 05:55:29 CEST 2008


New submission from Ezio Melotti <ezio.melotti at gmail.com>:

The re module seems to treat the raw strings as normal strings:
>>> 'a1a1a'.replace('1', r'\n') == re.sub('1', r'\n', 'a1a1a')
False
>>> 'a1a1a'.replace('1', '\n') == re.sub('1', r'\n', 'a1a1a')
True
In the first line str.replace and re.sub should perform exactly the same
operation and return the same result but re.sub replaces the 1s with
newlines, instead of a literal '\' and 'n'.
The second line should evaluate to False but re.sub replaces again the
1s with newlines so the result is equal to the LHS.

>>> re.search(r'\n', 'a\na')
<_sre.SRE_Match object at 0x00A81BF0>
>>> r'\n' in 'a\na'
False
Searching a r'\n' in a string that contains a newline also return a
result even if r'\n' is not in 'a\na'.

Tested with Py2.5 on Linux and Py2.4/2.6 on win.
The problem could be related to http://bugs.python.org/msg71861 
Attached there is a txt file with more examples.

----------
components: Library (Lib), Regular Expressions
files: raw-strings-with-re.txt
messages: 75133
nosy: ezio.melotti
severity: normal
status: open
title: re module treats raw strings as normal strings
type: behavior
versions: Python 2.4, Python 2.5, Python 2.5.3, Python 2.6
Added file: http://bugs.python.org/file11868/raw-strings-with-re.txt

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4185>
_______________________________________


More information about the Python-bugs-list mailing list