mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
bpo-29995: re.escape() now escapes only special characters. (#1007)
This commit is contained in:
parent
a6e395dffa
commit
5908300e4b
6 changed files with 40 additions and 51 deletions
|
|
@ -786,7 +786,7 @@ form.
|
|||
|
||||
.. function:: escape(pattern)
|
||||
|
||||
Escape all the characters in *pattern* except ASCII letters, numbers and ``'_'``.
|
||||
Escape special characters in *pattern*.
|
||||
This is useful if you want to match an arbitrary literal string that may
|
||||
have regular expression metacharacters in it. For example::
|
||||
|
||||
|
|
@ -795,15 +795,19 @@ form.
|
|||
|
||||
>>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:"
|
||||
>>> print('[%s]+' % re.escape(legal_chars))
|
||||
[abcdefghijklmnopqrstuvwxyz0123456789\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:]+
|
||||
[abcdefghijklmnopqrstuvwxyz0123456789!\#\$%&'\*\+\-\.\^_`\|~:]+
|
||||
|
||||
>>> operators = ['+', '-', '*', '/', '**']
|
||||
>>> print('|'.join(map(re.escape, sorted(operators, reverse=True))))
|
||||
\/|\-|\+|\*\*|\*
|
||||
/|\-|\+|\*\*|\*
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
The ``'_'`` character is no longer escaped.
|
||||
|
||||
.. versionchanged:: 3.7
|
||||
Only characters that can have special meaning in a regular expression
|
||||
are escaped.
|
||||
|
||||
|
||||
.. function:: purge()
|
||||
|
||||
|
|
|
|||
|
|
@ -303,7 +303,7 @@ whatsnew/3.2,,:gz,">>> with tarfile.open(name='myarchive.tar.gz', mode='w:gz') a
|
|||
whatsnew/3.2,,:location,zope9-location = ${zope9:location}
|
||||
whatsnew/3.2,,:prefix,zope-conf = ${custom:prefix}/etc/zope.conf
|
||||
library/re,,`,!#$%&'*+-.^_`|~:
|
||||
library/re,,`,\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:
|
||||
library/re,,`,!\#\$%&'\*\+\-\.\^_`\|~:
|
||||
library/tarfile,,:xz,'x:xz'
|
||||
library/xml.etree.elementtree,,:sometag,prefix:sometag
|
||||
library/xml.etree.elementtree,,:fictional,"<actors xmlns:fictional=""http://characters.example.com"""
|
||||
|
|
|
|||
|
Loading…
Add table
Add a link
Reference in a new issue