summaryrefslogtreecommitdiff
path: root/Doc/library/re.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/re.rst')
-rw-r--r--Doc/library/re.rst65
1 files changed, 47 insertions, 18 deletions
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index 1ca621eca6..7ef4cbeee7 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -230,12 +230,8 @@ The special characters are:
flags are described in :ref:`contents-of-module-re`.) This
is useful if you wish to include the flags as part of the regular
expression, instead of passing a *flag* argument to the
- :func:`re.compile` function.
-
- Note that the ``(?x)`` flag changes how the expression is parsed. It should be
- used first in the expression string, or after one or more whitespace characters.
- If there are non-whitespace characters before the flag, the results are
- undefined.
+ :func:`re.compile` function. Flags should be used first in the
+ expression string.
``(?:...)``
A non-capturing version of regular parentheses. Matches whatever regular
@@ -243,6 +239,16 @@ The special characters are:
*cannot* be retrieved after performing a match or referenced later in the
pattern.
+``(?imsx-imsx:...)``
+ (Zero or more letters from the set ``'i'``, ``'m'``, ``'s'``, ``'x'``,
+ optionally followed by ``'-'`` followed by one or more letters from the
+ same set.) The letters set or removes the corresponding flags:
+ :const:`re.I` (ignore case), :const:`re.M` (multi-line), :const:`re.S`
+ (dot matches all), and :const:`re.X` (verbose), for the part of the
+ expression. (The flags are described in :ref:`contents-of-module-re`.)
+
+ .. versionadded:: 3.6
+
``(?P<name>...)``
Similar to regular parentheses, but the substring matched by the group is
accessible via the symbolic group name *name*. Group names must be valid
@@ -327,8 +333,9 @@ The special characters are:
The special sequences consist of ``'\'`` and a character from the list below.
-If the ordinary character is not on the list, then the resulting RE will match
-the second character. For example, ``\$`` matches the character ``'$'``.
+If the ordinary character is not an ASCII digit or an ASCII letter, then the
+resulting RE will match the second character. For example, ``\$`` matches the
+character ``'$'``.
``\number``
Matches the contents of the group of the same number. Groups are numbered
@@ -448,9 +455,8 @@ three digits in length.
.. versionchanged:: 3.3
The ``'\u'`` and ``'\U'`` escape sequences have been added.
-.. deprecated-removed:: 3.5 3.6
- Unknown escapes consisting of ``'\'`` and ASCII letter now raise a
- deprecation warning and will be forbidden in Python 3.6.
+.. versionchanged:: 3.6
+ Unknown escapes consisting of ``'\'`` and an ASCII letter now are errors.
.. seealso::
@@ -472,6 +478,9 @@ functions are simplified versions of the full featured methods for compiled
regular expressions. Most non-trivial applications always use the compiled
form.
+.. versionchanged:: 3.6
+ Flag constants are now instances of :class:`RegexFlag`, which is a subclass of
+ :class:`enum.IntFlag`.
.. function:: compile(pattern, flags=0)
@@ -538,11 +547,11 @@ form.
current locale. The use of this flag is discouraged as the locale mechanism
is very unreliable, and it only handles one "culture" at a time anyway;
you should use Unicode matching instead, which is the default in Python 3
- for Unicode (str) patterns. This flag makes sense only with bytes patterns.
+ for Unicode (str) patterns. This flag can be used only with bytes patterns.
- .. deprecated-removed:: 3.5 3.6
- Deprecated the use of :const:`re.LOCALE` with string patterns or
- :const:`re.ASCII`.
+ .. versionchanged:: 3.6
+ :const:`re.LOCALE` can be used only with bytes patterns and is
+ not compatible with :const:`re.ASCII`.
.. data:: M
@@ -748,9 +757,13 @@ form.
.. versionchanged:: 3.5
Unmatched groups are replaced with an empty string.
- .. deprecated-removed:: 3.5 3.6
- Unknown escapes consist of ``'\'`` and ASCII letter now raise a
- deprecation warning and will be forbidden in Python 3.6.
+ .. versionchanged:: 3.6
+ Unknown escapes in *pattern* consisting of ``'\'`` and an ASCII letter
+ now are errors.
+
+ .. deprecated-removed:: 3.5 3.7
+ Unknown escapes in *repl* consisting of ``'\'`` and an ASCII letter now raise
+ a deprecation warning and will be forbidden in Python 3.7.
.. function:: subn(pattern, repl, string, count=0, flags=0)
@@ -1016,6 +1029,22 @@ Match objects support the following methods and attributes:
'c3'
+.. method:: match.__getitem__(g)
+
+ This is identical to ``m.group(g)``. This allows easier access to
+ an individual group from a match:
+
+ >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
+ >>> m[0] # The entire match
+ 'Isaac Newton'
+ >>> m[1] # The first parenthesized subgroup.
+ 'Isaac'
+ >>> m[2] # The second parenthesized subgroup.
+ 'Newton'
+
+ .. versionadded:: 3.6
+
+
.. method:: match.groups(default=None)
Return a tuple containing all the subgroups of the match, from 1 up to however