Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-30021: Add examples for re.escape().
  • Loading branch information
serhiy-storchaka committed Apr 8, 2017
commit b0c84fe6568663bf92fbf7ec340ca052f93cfabb
13 changes: 12 additions & 1 deletion Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down