diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-04-13 16:17:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-13 16:17:36 (GMT) |
commit | 8fc7bc2b7631ee819ee614e47b6f44bacebe1574 (patch) | |
tree | f965d27fbb8969301156bdda33b97ef631bf87d3 /Doc/library/re.rst | |
parent | 7b2491a6aa5cdc1f8f9e3fd9df91f29ee69aa982 (diff) | |
download | cpython-8fc7bc2b7631ee819ee614e47b6f44bacebe1574.zip cpython-8fc7bc2b7631ee819ee614e47b6f44bacebe1574.tar.gz cpython-8fc7bc2b7631ee819ee614e47b6f44bacebe1574.tar.bz2 |
bpo-30021: Add examples for re.escape(). (#1048)
And fix the parameter name.
Diffstat (limited to 'Doc/library/re.rst')
-rw-r--r-- | Doc/library/re.rst | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 323854a..3213daf 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. |