diff -r 9da300ad8255 Doc/library/re.rst --- a/Doc/library/re.rst Fri Mar 25 14:26:56 2011 +0200 +++ b/Doc/library/re.rst Fri Mar 25 15:22:18 2011 +0200 @@ -689,9 +689,9 @@ .. function:: escape(string) - Return *string* with all non-alphanumerics backslashed; this is useful if you - want to match an arbitrary literal string that may have regular expression - metacharacters in it. + Escape all the characters in pattern except ASCII letters, numbers and ``'_'``. + This is useful if you want to match an arbitrary literal string that may + have regular expression metacharacters in it. .. function:: purge() diff -r 9da300ad8255 Lib/re.py --- a/Lib/re.py Fri Mar 25 14:26:56 2011 +0200 +++ b/Lib/re.py Fri Mar 25 15:22:18 2011 +0200 @@ -215,12 +215,14 @@ return _compile(pattern, flags|T) _alphanum_str = frozenset( - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") + "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") _alphanum_bytes = frozenset( - b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") + b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") def escape(pattern): - "Escape all non-alphanumeric characters in pattern." + """ + Escape all the characters in pattern except ASCII letters, numbers and '_'. + """ if isinstance(pattern, str): alphanum = _alphanum_str s = list(pattern) diff -r 9da300ad8255 Lib/test/test_re.py --- a/Lib/test/test_re.py Fri Mar 25 14:26:56 2011 +0200 +++ b/Lib/test/test_re.py Fri Mar 25 15:22:18 2011 +0200 @@ -428,7 +428,7 @@ self.assertEqual(m.span(), span) def test_re_escape(self): - alnum_chars = string.ascii_letters + string.digits + alnum_chars = string.ascii_letters + string.digits + '_' p = ''.join(chr(i) for i in range(256)) for c in p: if c in alnum_chars: @@ -441,7 +441,7 @@ self.assertMatch(re.escape(p), p) def test_re_escape_byte(self): - alnum_chars = (string.ascii_letters + string.digits).encode('ascii') + alnum_chars = (string.ascii_letters + string.digits + '_').encode('ascii') p = bytes(range(256)) for i in p: b = bytes([i])