From b0c84fe6568663bf92fbf7ec340ca052f93cfabb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 8 Apr 2017 11:10:04 +0300 Subject: [PATCH 1/3] bpo-30021: Add examples for re.escape(). --- Doc/library/re.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 323854a2995ede..22d5cddf10816c 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -788,7 +788,18 @@ form. 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. + have regular expression metacharacters in it. For example:: + + >>> print(re.escape('python.exe')) + python\.exe + + >>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:" + >>> print('[%s]+' % re.escape(legal_chars)) + [abcdefghijklmnopqrstuvwxyz0123456789\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:]+ + + >>> operators = ['+', '-', '*', '/', '**'] + >>> print('|'.join(map(re.escape, sorted(operators, reverse=True)))) + \/|\-|\+|\*\*|\* .. versionchanged:: 3.3 The ``'_'`` character is no longer escaped. From 29be5c4658ebfcf8c73d9215380509cd2076b2b7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 12 Apr 2017 12:46:46 +0300 Subject: [PATCH 2/3] Add susp-ignored entries. --- Doc/tools/susp-ignored.csv | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv index 51edb6623820f9..df67f7590dc2e9 100644 --- a/Doc/tools/susp-ignored.csv +++ b/Doc/tools/susp-ignored.csv @@ -302,6 +302,8 @@ whatsnew/3.2,,:feed,>>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe: whatsnew/3.2,,:gz,">>> with tarfile.open(name='myarchive.tar.gz', mode='w:gz') as tf:" whatsnew/3.2,,:location,zope9-location = ${zope9:location} whatsnew/3.2,,:prefix,zope-conf = ${custom:prefix}/etc/zope.conf +library/re,,`,!#$%&'*+-.^_`|~: +library/re,,`,\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\: library/tarfile,,:xz,'x:xz' library/xml.etree.elementtree,,:sometag,prefix:sometag library/xml.etree.elementtree,,:fictional," Date: Thu, 13 Apr 2017 10:43:27 +0300 Subject: [PATCH 3/3] Fix the parameter name. --- Doc/library/re.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 22d5cddf10816c..3213daf6cfb6a0 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -784,9 +784,9 @@ form. Unmatched groups are replaced with an empty string. -.. function:: escape(string) +.. function:: escape(pattern) - Escape all the characters in pattern except ASCII letters, numbers and ``'_'``. + 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. For example::