@@ -289,6 +289,8 @@ Putting REs in strings keeps the Python language simpler, but has one
289289disadvantage which is the topic of the next section.
290290
291291
292+ .. _the-backslash-plague :
293+
292294The Backslash Plague
293295--------------------
294296
@@ -327,6 +329,13 @@ backslashes are not handled in any special way in a string literal prefixed with
327329while ``"\n" `` is a one-character string containing a newline. Regular
328330expressions will often be written in Python code using this raw string notation.
329331
332+ In addition, special escape sequences that are valid in regular expressions,
333+ but not valid as Python string literals, now result in a
334+ :exc: `DeprecationWarning ` and will eventually become a :exc: `SyntaxError `,
335+ which means the sequences will be invalid if raw string notation or escaping
336+ the backslashes isn't used.
337+
338+
330339+-------------------+------------------+
331340| Regular String | Raw string |
332341+===================+==================+
@@ -457,10 +466,16 @@ In actual programs, the most common style is to store the
457466Two pattern methods return all of the matches for a pattern.
458467:meth: `~re.Pattern.findall ` returns a list of matching strings::
459468
460- >>> p = re.compile('\d+')
469+ >>> p = re.compile(r '\d+')
461470 >>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')
462471 ['12', '11', '10']
463472
473+ The ``r `` prefix, making the literal a raw string literal, is needed in this
474+ example because escape sequences in a normal "cooked" string literal that are
475+ not recognized by Python, as opposed to regular expressions, now result in a
476+ :exc: `DeprecationWarning ` and will eventually become a :exc: `SyntaxError `. See
477+ :ref: `the-backslash-plague `.
478+
464479:meth: `~re.Pattern.findall ` has to create the entire list before it can be returned as the
465480result. The :meth: `~re.Pattern.finditer ` method returns a sequence of
466481:ref: `match object <match-objects >` instances as an :term: `iterator `::
@@ -1096,11 +1111,11 @@ following calls::
10961111The module-level function :func: `re.split ` adds the RE to be used as the first
10971112argument, but is otherwise the same. ::
10981113
1099- >>> re.split('[\W]+', 'Words, words, words.')
1114+ >>> re.split(r '[\W]+', 'Words, words, words.')
11001115 ['Words', 'words', 'words', '']
1101- >>> re.split('([\W]+)', 'Words, words, words.')
1116+ >>> re.split(r '([\W]+)', 'Words, words, words.')
11021117 ['Words', ', ', 'words', ', ', 'words', '.', '']
1103- >>> re.split('[\W]+', 'Words, words, words.', 1)
1118+ >>> re.split(r '[\W]+', 'Words, words, words.', 1)
11041119 ['Words', 'words, words.']
11051120
11061121
0 commit comments