summaryrefslogtreecommitdiffstats
path: root/Doc/library/re.rst
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-04-13 16:41:19 (GMT)
committerGitHub <noreply@github.com>2017-04-13 16:41:19 (GMT)
commit936633282220768a6fec9bd6bb53ee9e3ec354df (patch)
treeec763cf62842e856d850b16c818e22f871a0b9ad /Doc/library/re.rst
parent119d94ad37a99ecb0b8329467d04cd9d909e310e (diff)
downloadcpython-936633282220768a6fec9bd6bb53ee9e3ec354df.zip
cpython-936633282220768a6fec9bd6bb53ee9e3ec354df.tar.gz
cpython-936633282220768a6fec9bd6bb53ee9e3ec354df.tar.bz2
bpo-30021: Add examples for re.escape(). (#1048) (#1115)
And fix the parameter name. (cherry picked from commit 8fc7bc2b7631ee819ee614e47b6f44bacebe1574)
Diffstat (limited to 'Doc/library/re.rst')
-rw-r--r--Doc/library/re.rst17
1 files changed, 14 insertions, 3 deletions
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index 9cced51..944c6fb 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -784,11 +784,22 @@ 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.
+ 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.