mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge latest revs by Mark.
This commit is contained in:
parent
f60281302e
commit
f61b28a73d
@ -9,7 +9,8 @@
|
|||||||
.. versionadded:: 2.5
|
.. versionadded:: 2.5
|
||||||
|
|
||||||
This module provides utilities for common tasks involving the :keyword:`with`
|
This module provides utilities for common tasks involving the :keyword:`with`
|
||||||
statement.
|
statement. For more information see also :ref:`typecontextmanager` and
|
||||||
|
:ref:`context-managers`.
|
||||||
|
|
||||||
Functions provided:
|
Functions provided:
|
||||||
|
|
||||||
|
@ -10,19 +10,19 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
This module provides regular expression matching operations similar to those
|
This module provides regular expression matching operations similar to
|
||||||
found in Perl. Regular expression pattern strings may not contain null bytes,
|
those found in Perl. Both patterns and strings to be searched can be
|
||||||
but can specify the null byte using the ``\number`` notation. Both patterns and
|
Unicode strings as well as 8-bit strings. The :mod:`re` module is
|
||||||
strings to be searched can be Unicode strings as well as 8-bit strings. The
|
always available.
|
||||||
:mod:`re` module is always available.
|
|
||||||
|
|
||||||
Regular expressions use the backslash character (``'\'``) to indicate special
|
Regular expressions use the backslash character (``'\'``) to indicate
|
||||||
forms or to allow special characters to be used without invoking their special
|
special forms or to allow special characters to be used without invoking
|
||||||
meaning. This collides with Python's usage of the same character for the same
|
their special meaning. This collides with Python's usage of the same
|
||||||
purpose in string literals; for example, to match a literal backslash, one might
|
character for the same purpose in string literals; for example, to match
|
||||||
have to write ``'\\\\'`` as the pattern string, because the regular expression
|
a literal backslash, one might have to write ``'\\\\'`` as the pattern
|
||||||
must be ``\\``, and each backslash must be expressed as ``\\`` inside a regular
|
string, because the regular expression must be ``\\``, and each
|
||||||
Python string literal.
|
backslash must be expressed as ``\\`` inside a regular Python string
|
||||||
|
literal.
|
||||||
|
|
||||||
The solution is to use Python's raw string notation for regular expression
|
The solution is to use Python's raw string notation for regular expression
|
||||||
patterns; backslashes are not handled in any special way in a string literal
|
patterns; backslashes are not handled in any special way in a string literal
|
||||||
@ -31,7 +31,6 @@ prefixed with ``'r'``. So ``r"\n"`` is a two-character string containing
|
|||||||
newline. Usually patterns will be expressed in Python code using this raw string
|
newline. Usually patterns will be expressed in Python code using this raw string
|
||||||
notation.
|
notation.
|
||||||
|
|
||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
||||||
Mastering Regular Expressions
|
Mastering Regular Expressions
|
||||||
@ -71,9 +70,12 @@ characters, so ``last`` matches the string ``'last'``. (In the rest of this
|
|||||||
section, we'll write RE's in ``this special style``, usually without quotes, and
|
section, we'll write RE's in ``this special style``, usually without quotes, and
|
||||||
strings to be matched ``'in single quotes'``.)
|
strings to be matched ``'in single quotes'``.)
|
||||||
|
|
||||||
Some characters, like ``'|'`` or ``'('``, are special. Special characters either
|
Some characters, like ``'|'`` or ``'('``, are special. Special
|
||||||
stand for classes of ordinary characters, or affect how the regular expressions
|
characters either stand for classes of ordinary characters, or affect
|
||||||
around them are interpreted.
|
how the regular expressions around them are interpreted. Regular
|
||||||
|
expression pattern strings may not contain null bytes, but can specify
|
||||||
|
the null byte using the ``\number`` notation, e.g., ``'\x00'``.
|
||||||
|
|
||||||
|
|
||||||
The special characters are:
|
The special characters are:
|
||||||
|
|
||||||
@ -156,12 +158,15 @@ The special characters are:
|
|||||||
Used to indicate a set of characters. Characters can be listed individually, or
|
Used to indicate a set of characters. Characters can be listed individually, or
|
||||||
a range of characters can be indicated by giving two characters and separating
|
a range of characters can be indicated by giving two characters and separating
|
||||||
them by a ``'-'``. Special characters are not active inside sets. For example,
|
them by a ``'-'``. Special characters are not active inside sets. For example,
|
||||||
``[akm$]`` will match any of the characters ``'a'``, ``'k'``, ``'m'``, or
|
``[akm$]`` will match any of the characters ``'a'``, ``'k'``,
|
||||||
``'$'``; ``[a-z]`` will match any lowercase letter, and ``[a-zA-Z0-9]`` matches
|
``'m'``, or ``'$'``; ``[a-z]`` will match any lowercase letter, and
|
||||||
any letter or digit. Character classes such as ``\w`` or ``\S`` (defined below)
|
``[a-zA-Z0-9]`` matches any letter or digit. Character classes such
|
||||||
are also acceptable inside a range. If you want to include a ``']'`` or a
|
as ``\w`` or ``\S`` (defined below) are also acceptable inside a
|
||||||
``'-'`` inside a set, precede it with a backslash, or place it as the first
|
range, although the characters they match depends on whether :const:`LOCALE`
|
||||||
character. The pattern ``[]]`` will match ``']'``, for example.
|
or :const:`UNICODE` mode is in force. If you want to include a
|
||||||
|
``']'`` or a ``'-'`` inside a set, precede it with a backslash, or
|
||||||
|
place it as the first character. The pattern ``[]]`` will match
|
||||||
|
``']'``, for example.
|
||||||
|
|
||||||
You can match the characters not within a range by :dfn:`complementing` the set.
|
You can match the characters not within a range by :dfn:`complementing` the set.
|
||||||
This is indicated by including a ``'^'`` as the first character of the set;
|
This is indicated by including a ``'^'`` as the first character of the set;
|
||||||
@ -195,12 +200,16 @@ The special characters are:
|
|||||||
currently supported extensions.
|
currently supported extensions.
|
||||||
|
|
||||||
``(?iLmsux)``
|
``(?iLmsux)``
|
||||||
(One or more letters from the set ``'i'``, ``'L'``, ``'m'``, ``'s'``, ``'u'``,
|
(One or more letters from the set ``'i'``, ``'L'``, ``'m'``, ``'s'``,
|
||||||
``'x'``.) The group matches the empty string; the letters set the corresponding
|
``'u'``, ``'x'``.) The group matches the empty string; the letters
|
||||||
flags (:const:`re.I`, :const:`re.L`, :const:`re.M`, :const:`re.S`,
|
set the corresponding flags: :const:`re.I` (ignore case),
|
||||||
:const:`re.U`, :const:`re.X`) for the entire regular expression. This is useful
|
:const:`re.L` (locale dependent), :const:`re.M` (multi-line),
|
||||||
if you wish to include the flags as part of the regular expression, instead of
|
:const:`re.S` (dot matches all), :const:`re.U` (Unicode dependent),
|
||||||
passing a *flag* argument to the :func:`compile` function.
|
and :const:`re.X` (verbose), for the entire regular expression. (The
|
||||||
|
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:`compile` function.
|
||||||
|
|
||||||
Note that the ``(?x)`` flag changes how the expression is parsed. It should be
|
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.
|
used first in the expression string, or after one or more whitespace characters.
|
||||||
@ -218,7 +227,7 @@ The special characters are:
|
|||||||
accessible via the symbolic group name *name*. Group names must be valid Python
|
accessible via the symbolic group name *name*. Group names must be valid Python
|
||||||
identifiers, and each group name must be defined only once within a regular
|
identifiers, and each group name must be defined only once within a regular
|
||||||
expression. A symbolic group is also a numbered group, just as if the group
|
expression. A symbolic group is also a numbered group, just as if the group
|
||||||
were not named. So the group named 'id' in the example above can also be
|
were not named. So the group named 'id' in the example below can also be
|
||||||
referenced as the numbered group 1.
|
referenced as the numbered group 1.
|
||||||
|
|
||||||
For example, if the pattern is ``(?P<id>[a-zA-Z_]\w*)``, the group can be
|
For example, if the pattern is ``(?P<id>[a-zA-Z_]\w*)``, the group can be
|
||||||
@ -273,7 +282,7 @@ The special characters are:
|
|||||||
|
|
||||||
``(?(id/name)yes-pattern|no-pattern)``
|
``(?(id/name)yes-pattern|no-pattern)``
|
||||||
Will try to match with ``yes-pattern`` if the group with given *id* or *name*
|
Will try to match with ``yes-pattern`` if the group with given *id* or *name*
|
||||||
exists, and with ``no-pattern`` if it doesn't. ``|no-pattern`` is optional and
|
exists, and with ``no-pattern`` if it doesn't. ``no-pattern`` is optional and
|
||||||
can be omitted. For example, ``(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)`` is a poor email
|
can be omitted. For example, ``(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)`` is a poor email
|
||||||
matching pattern, which will match with ``'<user@host.com>'`` as well as
|
matching pattern, which will match with ``'<user@host.com>'`` as well as
|
||||||
``'user@host.com'``, but not with ``'<user@host.com'``.
|
``'user@host.com'``, but not with ``'<user@host.com'``.
|
||||||
@ -310,7 +319,7 @@ the second character. For example, ``\$`` matches the character ``'$'``.
|
|||||||
|
|
||||||
``\B``
|
``\B``
|
||||||
Matches the empty string, but only when it is *not* at the beginning or end of a
|
Matches the empty string, but only when it is *not* at the beginning or end of a
|
||||||
word. This is just the opposite of ``\ b``, so is also subject to the settings
|
word. This is just the opposite of ``\b``, so is also subject to the settings
|
||||||
of ``LOCALE`` and ``UNICODE``.
|
of ``LOCALE`` and ``UNICODE``.
|
||||||
|
|
||||||
``\d``
|
``\d``
|
||||||
|
@ -2131,7 +2131,8 @@ to be provided for a context manager object to define a runtime context:
|
|||||||
Python defines several context managers to support easy thread synchronisation,
|
Python defines several context managers to support easy thread synchronisation,
|
||||||
prompt closure of files or other objects, and simpler manipulation of the active
|
prompt closure of files or other objects, and simpler manipulation of the active
|
||||||
decimal arithmetic context. The specific types are not treated specially beyond
|
decimal arithmetic context. The specific types are not treated specially beyond
|
||||||
their implementation of the context management protocol.
|
their implementation of the context management protocol. See the
|
||||||
|
:mod:`contextlib` module for some examples.
|
||||||
|
|
||||||
Python's generators and the ``contextlib.contextfactory`` decorator provide a
|
Python's generators and the ``contextlib.contextfactory`` decorator provide a
|
||||||
convenient way to implement these protocols. If a generator function is
|
convenient way to implement these protocols. If a generator function is
|
||||||
@ -2261,7 +2262,7 @@ executable Python code such as a function body. They differ from function
|
|||||||
objects because they don't contain a reference to their global execution
|
objects because they don't contain a reference to their global execution
|
||||||
environment. Code objects are returned by the built-in :func:`compile` function
|
environment. Code objects are returned by the built-in :func:`compile` function
|
||||||
and can be extracted from function objects through their :attr:`__code__`
|
and can be extracted from function objects through their :attr:`__code__`
|
||||||
attribute.
|
attribute. See also the :mod:`code` module.
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
builtin: exec
|
builtin: exec
|
||||||
|
@ -35,7 +35,7 @@ The module defines the following exception and functions:
|
|||||||
|
|
||||||
Pack the values ``v1, v2, ...`` according to the given format, write the packed
|
Pack the values ``v1, v2, ...`` according to the given format, write the packed
|
||||||
bytes into the writable *buffer* starting at *offset*. Note that the offset is
|
bytes into the writable *buffer* starting at *offset*. Note that the offset is
|
||||||
not an optional argument.
|
a required argument.
|
||||||
|
|
||||||
.. versionadded:: 2.5
|
.. versionadded:: 2.5
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user