Skip to content

Commit 53ad684

Browse files
bpo-30021: Add examples for re.escape(). (#1048) (#1118)
And fix the parameter name. (cherry picked from commit 8fc7bc2)
1 parent 65c5b09 commit 53ad684

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

Doc/library/re.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,22 @@ form.
695695
Added the optional flags argument.
696696

697697

698-
.. function:: escape(string)
698+
.. function:: escape(pattern)
699699

700-
Return *string* with all non-alphanumerics backslashed; this is useful if you
701-
want to match an arbitrary literal string that may have regular expression
702-
metacharacters in it.
700+
Escape all the characters in *pattern* except ASCII letters and numbers.
701+
This is useful if you want to match an arbitrary literal string that may
702+
have regular expression metacharacters in it. For example::
703+
704+
>>> print re.escape('python.exe')
705+
python\.exe
706+
707+
>>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:"
708+
>>> print '[%s]+' % re.escape(legal_chars)
709+
[abcdefghijklmnopqrstuvwxyz0123456789\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~\:]+
710+
711+
>>> operators = ['+', '-', '*', '/', '**']
712+
>>> print '|'.join(map(re.escape, sorted(operators, reverse=True)))
713+
\/|\-|\+|\*\*|\*
703714

704715

705716
.. function:: purge()

Doc/tools/susp-ignored.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ library/profile,,:lineno,ncalls tottime percall cumtime percall filename:lin
9999
library/profile,,:lineno,filename:lineno(function)
100100
library/pyexpat,,:elem1,<py:elem1 />
101101
library/pyexpat,,:py,"xmlns:py = ""http://www.python.org/ns/"">"
102+
library/re,,`,!#$%&'*+-.^_`|~:
103+
library/re,,`,\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:
102104
library/smtplib,,:port,method must support that as well as a regular host:port
103105
library/socket,,::,'5aef:2b::8'
104106
library/sqlite3,,:memory,

0 commit comments

Comments
 (0)