Skip to content

Commit 1557da6

Browse files
serhiy-storchakavadmiumhugovk
authored
pythongh-106482: Clarify documentation of character set in RE (python#106517)
Co-authored-by: Martin Panter <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 5fbe23e commit 1557da6

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Doc/library/re.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,23 @@ The special characters are:
250250
``[a\-z]``) or if it's placed as the first or last character
251251
(e.g. ``[-a]`` or ``[a-]``), it will match a literal ``'-'``.
252252

253-
* Special characters lose their special meaning inside sets. For example,
253+
* Special characters except backslash lose their special meaning inside sets.
254+
For example,
254255
``[(+*)]`` will match any of the literal characters ``'('``, ``'+'``,
255256
``'*'``, or ``')'``.
256257

257258
.. index:: single: \ (backslash); in regular expressions
258259

259-
* Character classes such as ``\w`` or ``\S`` (defined below) are also accepted
260-
inside a set, although the characters they match depend on the flags_ used.
260+
* Backslash either escapes characters which have special meaning in a set
261+
such as ``'-'``, ``']'``, ``'^'`` and ``'\\'`` itself or signals
262+
a special sequence which represents a single character such as
263+
``\xa0`` or ``\n`` or a character class such as ``\w`` or ``\S``
264+
(defined below).
265+
Note that ``\b`` represents a single "backspace" character,
266+
not a word boundary as outside a set, and numeric escapes
267+
such as ``\1`` are always octal escapes, not group references.
268+
Special sequences which do not match a single character such as ``\A``
269+
and ``\Z`` are not allowed.
261270

262271
.. index:: single: ^ (caret); in regular expressions
263272

0 commit comments

Comments
 (0)