2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
:mod:`re` --- Regular expression operations
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
.. module:: re
|
|
|
|
:synopsis: Regular expression operations.
|
|
|
|
.. moduleauthor:: Fredrik Lundh <fredrik@pythonware.com>
|
|
|
|
.. sectionauthor:: Andrew M. Kuchling <amk@amk.ca>
|
|
|
|
|
|
|
|
|
|
|
|
This module provides regular expression matching operations similar to
|
|
|
|
those found in Perl. Both patterns and strings to be searched can be
|
Merged revisions 70642,70648,70656,70661,70765,70773,70789,70824-70825,70828,70830,70832,70836,70838,70842,70851,70855,70857-70858 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70642 | georg.brandl | 2009-03-28 01:48:48 +0100 (Sa, 28 Mär 2009) | 1 line
Fix typo.
........
r70648 | georg.brandl | 2009-03-28 20:10:37 +0100 (Sa, 28 Mär 2009) | 1 line
#5324: document __subclasses__().
........
r70656 | georg.brandl | 2009-03-28 20:33:33 +0100 (Sa, 28 Mär 2009) | 2 lines
Add a script to fixup rst files if the pre-commit hook rejects them.
........
r70661 | georg.brandl | 2009-03-28 20:57:36 +0100 (Sa, 28 Mär 2009) | 2 lines
Add section numbering to some of the larger subdocuments.
........
r70765 | georg.brandl | 2009-03-31 00:09:34 +0200 (Di, 31 Mär 2009) | 1 line
#5199: make warning about vars() assignment more visible.
........
r70773 | georg.brandl | 2009-03-31 00:43:00 +0200 (Di, 31 Mär 2009) | 1 line
#5039: make it clear that the impl. note refers to CPython.
........
r70789 | georg.brandl | 2009-03-31 03:25:15 +0200 (Di, 31 Mär 2009) | 1 line
Fix a wrong struct field assignment (docstring as closure).
........
r70824 | georg.brandl | 2009-03-31 17:43:20 +0200 (Di, 31 Mär 2009) | 1 line
#5519: remove reference to Kodos, which seems dead.
........
r70825 | georg.brandl | 2009-03-31 17:46:30 +0200 (Di, 31 Mär 2009) | 1 line
#5566: fix versionadded from PyLong ssize_t functions.
........
r70828 | georg.brandl | 2009-03-31 17:50:16 +0200 (Di, 31 Mär 2009) | 1 line
#5581: fget argument of abstractproperty is optional as well.
........
r70830 | georg.brandl | 2009-03-31 18:11:45 +0200 (Di, 31 Mär 2009) | 1 line
#5529: backport new docs of import semantics written by Brett to 2.x.
........
r70832 | georg.brandl | 2009-03-31 18:31:11 +0200 (Di, 31 Mär 2009) | 1 line
#1386675: specify WindowsError as the exception, because it has a winerror attribute that EnvironmentError doesnt have.
........
r70836 | georg.brandl | 2009-03-31 18:50:25 +0200 (Di, 31 Mär 2009) | 1 line
#5417: replace references to undocumented functions by ones to documented functions.
........
r70838 | georg.brandl | 2009-03-31 18:54:38 +0200 (Di, 31 Mär 2009) | 1 line
#992207: document that the parser only accepts \\n newlines.
........
r70842 | georg.brandl | 2009-03-31 19:13:06 +0200 (Di, 31 Mär 2009) | 1 line
#970783: document PyObject_Generic[GS]etAttr.
........
r70851 | georg.brandl | 2009-03-31 20:26:55 +0200 (Di, 31 Mär 2009) | 1 line
#837577: note cryptic return value of spawn*e on invalid env dicts.
........
r70855 | georg.brandl | 2009-03-31 20:30:37 +0200 (Di, 31 Mär 2009) | 1 line
#5245: note that PyRun_SimpleString doesnt return on SystemExit.
........
r70857 | georg.brandl | 2009-03-31 20:33:10 +0200 (Di, 31 Mär 2009) | 1 line
#5227: note that Py_Main doesnt return on SystemExit.
........
r70858 | georg.brandl | 2009-03-31 20:38:56 +0200 (Di, 31 Mär 2009) | 1 line
#5241: document missing U in regex howto.
........
2009-04-05 18:11:43 -03:00
|
|
|
Unicode strings as well as 8-bit strings.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Regular expressions use the backslash character (``'\'``) to indicate
|
|
|
|
special forms or to allow special characters to be used without invoking
|
|
|
|
their special meaning. This collides with Python's usage of the same
|
|
|
|
character for the same purpose in string literals; for example, to match
|
|
|
|
a literal backslash, one might have to write ``'\\\\'`` as the pattern
|
|
|
|
string, because the regular expression must be ``\\``, and each
|
|
|
|
backslash must be expressed as ``\\`` inside a regular Python string
|
|
|
|
literal.
|
|
|
|
|
|
|
|
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
|
|
|
|
prefixed with ``'r'``. So ``r"\n"`` is a two-character string containing
|
|
|
|
``'\'`` and ``'n'``, while ``"\n"`` is a one-character string containing a
|
2007-09-27 03:26:58 -03:00
|
|
|
newline. Usually patterns will be expressed in Python code using this raw
|
|
|
|
string notation.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
It is important to note that most regular expression operations are available as
|
|
|
|
module-level functions and :class:`RegexObject` methods. The functions are
|
|
|
|
shortcuts that don't require you to compile a regex object first, but miss some
|
|
|
|
fine-tuning parameters.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Mastering Regular Expressions
|
|
|
|
Book on regular expressions by Jeffrey Friedl, published by O'Reilly. The
|
2007-09-27 03:26:58 -03:00
|
|
|
second edition of the book no longer covers Python at all, but the first
|
2007-08-15 11:28:01 -03:00
|
|
|
edition covered writing good regular expression patterns in great detail.
|
|
|
|
|
|
|
|
|
|
|
|
.. _re-syntax:
|
|
|
|
|
|
|
|
Regular Expression Syntax
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
A regular expression (or RE) specifies a set of strings that matches it; the
|
|
|
|
functions in this module let you check if a particular string matches a given
|
|
|
|
regular expression (or if a given regular expression matches a particular
|
|
|
|
string, which comes down to the same thing).
|
|
|
|
|
|
|
|
Regular expressions can be concatenated to form new regular expressions; if *A*
|
|
|
|
and *B* are both regular expressions, then *AB* is also a regular expression.
|
|
|
|
In general, if a string *p* matches *A* and another string *q* matches *B*, the
|
|
|
|
string *pq* will match AB. This holds unless *A* or *B* contain low precedence
|
|
|
|
operations; boundary conditions between *A* and *B*; or have numbered group
|
|
|
|
references. Thus, complex expressions can easily be constructed from simpler
|
|
|
|
primitive expressions like the ones described here. For details of the theory
|
|
|
|
and implementation of regular expressions, consult the Friedl book referenced
|
|
|
|
above, or almost any textbook about compiler construction.
|
|
|
|
|
|
|
|
A brief explanation of the format of regular expressions follows. For further
|
2008-02-05 08:01:24 -04:00
|
|
|
information and a gentler presentation, consult the :ref:`regex-howto`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Regular expressions can contain both special and ordinary characters. Most
|
|
|
|
ordinary characters, like ``'A'``, ``'a'``, or ``'0'``, are the simplest regular
|
|
|
|
expressions; they simply match themselves. You can concatenate ordinary
|
|
|
|
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
|
|
|
|
strings to be matched ``'in single quotes'``.)
|
|
|
|
|
|
|
|
Some characters, like ``'|'`` or ``'('``, are special. Special
|
|
|
|
characters either stand for classes of ordinary characters, or affect
|
|
|
|
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:
|
|
|
|
|
|
|
|
``'.'``
|
|
|
|
(Dot.) In the default mode, this matches any character except a newline. If
|
|
|
|
the :const:`DOTALL` flag has been specified, this matches any character
|
|
|
|
including a newline.
|
|
|
|
|
|
|
|
``'^'``
|
|
|
|
(Caret.) Matches the start of the string, and in :const:`MULTILINE` mode also
|
|
|
|
matches immediately after each newline.
|
|
|
|
|
|
|
|
``'$'``
|
|
|
|
Matches the end of the string or just before the newline at the end of the
|
|
|
|
string, and in :const:`MULTILINE` mode also matches before a newline. ``foo``
|
|
|
|
matches both 'foo' and 'foobar', while the regular expression ``foo$`` matches
|
|
|
|
only 'foo'. More interestingly, searching for ``foo.$`` in ``'foo1\nfoo2\n'``
|
2008-01-10 17:59:42 -04:00
|
|
|
matches 'foo2' normally, but 'foo1' in :const:`MULTILINE` mode; searching for
|
|
|
|
a single ``$`` in ``'foo\n'`` will find two (empty) matches: one just before
|
|
|
|
the newline, and one at the end of the string.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
``'*'``
|
|
|
|
Causes the resulting RE to match 0 or more repetitions of the preceding RE, as
|
|
|
|
many repetitions as are possible. ``ab*`` will match 'a', 'ab', or 'a' followed
|
|
|
|
by any number of 'b's.
|
|
|
|
|
|
|
|
``'+'``
|
|
|
|
Causes the resulting RE to match 1 or more repetitions of the preceding RE.
|
|
|
|
``ab+`` will match 'a' followed by any non-zero number of 'b's; it will not
|
|
|
|
match just 'a'.
|
|
|
|
|
|
|
|
``'?'``
|
|
|
|
Causes the resulting RE to match 0 or 1 repetitions of the preceding RE.
|
|
|
|
``ab?`` will match either 'a' or 'ab'.
|
|
|
|
|
|
|
|
``*?``, ``+?``, ``??``
|
|
|
|
The ``'*'``, ``'+'``, and ``'?'`` qualifiers are all :dfn:`greedy`; they match
|
|
|
|
as much text as possible. Sometimes this behaviour isn't desired; if the RE
|
|
|
|
``<.*>`` is matched against ``'<H1>title</H1>'``, it will match the entire
|
|
|
|
string, and not just ``'<H1>'``. Adding ``'?'`` after the qualifier makes it
|
|
|
|
perform the match in :dfn:`non-greedy` or :dfn:`minimal` fashion; as *few*
|
|
|
|
characters as possible will be matched. Using ``.*?`` in the previous
|
|
|
|
expression will match only ``'<H1>'``.
|
|
|
|
|
|
|
|
``{m}``
|
|
|
|
Specifies that exactly *m* copies of the previous RE should be matched; fewer
|
|
|
|
matches cause the entire RE not to match. For example, ``a{6}`` will match
|
|
|
|
exactly six ``'a'`` characters, but not five.
|
|
|
|
|
|
|
|
``{m,n}``
|
|
|
|
Causes the resulting RE to match from *m* to *n* repetitions of the preceding
|
|
|
|
RE, attempting to match as many repetitions as possible. For example,
|
|
|
|
``a{3,5}`` will match from 3 to 5 ``'a'`` characters. Omitting *m* specifies a
|
|
|
|
lower bound of zero, and omitting *n* specifies an infinite upper bound. As an
|
|
|
|
example, ``a{4,}b`` will match ``aaaab`` or a thousand ``'a'`` characters
|
|
|
|
followed by a ``b``, but not ``aaab``. The comma may not be omitted or the
|
|
|
|
modifier would be confused with the previously described form.
|
|
|
|
|
|
|
|
``{m,n}?``
|
|
|
|
Causes the resulting RE to match from *m* to *n* repetitions of the preceding
|
|
|
|
RE, attempting to match as *few* repetitions as possible. This is the
|
|
|
|
non-greedy version of the previous qualifier. For example, on the
|
|
|
|
6-character string ``'aaaaaa'``, ``a{3,5}`` will match 5 ``'a'`` characters,
|
|
|
|
while ``a{3,5}?`` will only match 3 characters.
|
|
|
|
|
|
|
|
``'\'``
|
|
|
|
Either escapes special characters (permitting you to match characters like
|
|
|
|
``'*'``, ``'?'``, and so forth), or signals a special sequence; special
|
|
|
|
sequences are discussed below.
|
|
|
|
|
|
|
|
If you're not using a raw string to express the pattern, remember that Python
|
|
|
|
also uses the backslash as an escape sequence in string literals; if the escape
|
|
|
|
sequence isn't recognized by Python's parser, the backslash and subsequent
|
|
|
|
character are included in the resulting string. However, if Python would
|
|
|
|
recognize the resulting sequence, the backslash should be repeated twice. This
|
|
|
|
is complicated and hard to understand, so it's highly recommended that you use
|
|
|
|
raw strings for all but the simplest expressions.
|
|
|
|
|
|
|
|
``[]``
|
|
|
|
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
|
|
|
|
them by a ``'-'``. Special characters are not active inside sets. For example,
|
|
|
|
``[akm$]`` will match any of the characters ``'a'``, ``'k'``,
|
|
|
|
``'m'``, or ``'$'``; ``[a-z]`` will match any lowercase letter, and
|
|
|
|
``[a-zA-Z0-9]`` matches any letter or digit. Character classes such
|
|
|
|
as ``\w`` or ``\S`` (defined below) are also acceptable inside a
|
|
|
|
range, although the characters they match depends on whether :const:`LOCALE`
|
|
|
|
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.
|
|
|
|
This is indicated by including a ``'^'`` as the first character of the set;
|
|
|
|
``'^'`` elsewhere will simply match the ``'^'`` character. For example,
|
|
|
|
``[^5]`` will match any character except ``'5'``, and ``[^^]`` will match any
|
|
|
|
character except ``'^'``.
|
|
|
|
|
2008-05-31 10:05:34 -03:00
|
|
|
Note that inside ``[]`` the special forms and special characters lose
|
|
|
|
their meanings and only the syntaxes described here are valid. For
|
|
|
|
example, ``+``, ``*``, ``(``, ``)``, and so on are treated as
|
|
|
|
literals inside ``[]``, and backreferences cannot be used inside
|
|
|
|
``[]``.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
``'|'``
|
|
|
|
``A|B``, where A and B can be arbitrary REs, creates a regular expression that
|
|
|
|
will match either A or B. An arbitrary number of REs can be separated by the
|
|
|
|
``'|'`` in this way. This can be used inside groups (see below) as well. As
|
|
|
|
the target string is scanned, REs separated by ``'|'`` are tried from left to
|
|
|
|
right. When one pattern completely matches, that branch is accepted. This means
|
|
|
|
that once ``A`` matches, ``B`` will not be tested further, even if it would
|
|
|
|
produce a longer overall match. In other words, the ``'|'`` operator is never
|
|
|
|
greedy. To match a literal ``'|'``, use ``\|``, or enclose it inside a
|
|
|
|
character class, as in ``[|]``.
|
|
|
|
|
|
|
|
``(...)``
|
|
|
|
Matches whatever regular expression is inside the parentheses, and indicates the
|
|
|
|
start and end of a group; the contents of a group can be retrieved after a match
|
|
|
|
has been performed, and can be matched later in the string with the ``\number``
|
|
|
|
special sequence, described below. To match the literals ``'('`` or ``')'``,
|
|
|
|
use ``\(`` or ``\)``, or enclose them inside a character class: ``[(] [)]``.
|
|
|
|
|
|
|
|
``(?...)``
|
|
|
|
This is an extension notation (a ``'?'`` following a ``'('`` is not meaningful
|
|
|
|
otherwise). The first character after the ``'?'`` determines what the meaning
|
|
|
|
and further syntax of the construct is. Extensions usually do not create a new
|
|
|
|
group; ``(?P<name>...)`` is the only exception to this rule. Following are the
|
|
|
|
currently supported extensions.
|
|
|
|
|
|
|
|
``(?iLmsux)``
|
|
|
|
(One or more letters from the set ``'i'``, ``'L'``, ``'m'``, ``'s'``,
|
|
|
|
``'u'``, ``'x'``.) The group matches the empty string; the letters
|
|
|
|
set the corresponding flags: :const:`re.I` (ignore case),
|
|
|
|
:const:`re.L` (locale dependent), :const:`re.M` (multi-line),
|
|
|
|
:const:`re.S` (dot matches all), :const:`re.U` (Unicode dependent),
|
|
|
|
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
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
:func:`re.compile` function.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
``(?:...)``
|
|
|
|
A non-grouping version of regular parentheses. Matches whatever regular
|
|
|
|
expression is inside the parentheses, but the substring matched by the group
|
|
|
|
*cannot* be retrieved after performing a match or referenced later in the
|
|
|
|
pattern.
|
|
|
|
|
|
|
|
``(?P<name>...)``
|
|
|
|
Similar to regular parentheses, but the substring matched by the group is
|
Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70866 | georg.brandl | 2009-03-31 21:06:57 +0200 (Di, 31 Mär 2009) | 1 line
#4882: document named group behavior a bit better.
........
r70867 | georg.brandl | 2009-03-31 21:10:35 +0200 (Di, 31 Mär 2009) | 1 line
#1096310: document usage of sys.__std*__ a bit better.
........
r70868 | georg.brandl | 2009-03-31 21:12:17 +0200 (Di, 31 Mär 2009) | 1 line
#5190: export make_option in __all__.
........
r70870 | georg.brandl | 2009-03-31 21:26:24 +0200 (Di, 31 Mär 2009) | 1 line
#4411: document mro() and __mro__. (I hope I got it right.)
........
r70871 | georg.brandl | 2009-03-31 21:30:56 +0200 (Di, 31 Mär 2009) | 1 line
#5618: fix typo.
........
r70893 | georg.brandl | 2009-03-31 22:56:32 +0200 (Di, 31 Mär 2009) | 1 line
#1530012: move TQS section before raw strings.
........
r70896 | georg.brandl | 2009-03-31 23:15:33 +0200 (Di, 31 Mär 2009) | 1 line
#5598: document DocFileSuite *args argument.
........
r70902 | georg.brandl | 2009-03-31 23:43:03 +0200 (Di, 31 Mär 2009) | 1 line
#1675026: add a note about a strange Windows problem, and remove notes about AtheOS.
........
r70905 | georg.brandl | 2009-04-01 00:03:40 +0200 (Mi, 01 Apr 2009) | 1 line
#5563: more documentation for bdist_msi.
........
r70907 | georg.brandl | 2009-04-01 00:18:19 +0200 (Mi, 01 Apr 2009) | 1 line
#3427: document correct return type for urlopen().info().
........
r70912 | georg.brandl | 2009-04-01 00:35:46 +0200 (Mi, 01 Apr 2009) | 1 line
#5617: add a handy function to print a unicode string to gdbinit.
........
r70915 | georg.brandl | 2009-04-01 00:40:16 +0200 (Mi, 01 Apr 2009) | 1 line
#5018: remove confusing paragraph.
........
r70927 | georg.brandl | 2009-04-01 01:01:27 +0200 (Mi, 01 Apr 2009) | 1 line
Dont shout to users.
........
r70933 | georg.brandl | 2009-04-01 02:04:33 +0200 (Mi, 01 Apr 2009) | 2 lines
Issue #5635: Fix running test_sys with tracing enabled.
........
r70940 | georg.brandl | 2009-04-01 06:21:14 +0200 (Mi, 01 Apr 2009) | 2 lines
The SimpleXMLRPCServer's CGI handler now runs like a pony.
........
r70944 | georg.brandl | 2009-04-01 06:32:39 +0200 (Mi, 01 Apr 2009) | 1 line
#5631: add upload to list of possible commands, which is presented in --help-commands.
........
r70954 | georg.brandl | 2009-04-01 17:23:43 +0200 (Mi, 01 Apr 2009) | 1 line
Fix test_xmlrpc and make the CGI handler work with no CONTENT_LENGTH.
........
r70963 | georg.brandl | 2009-04-01 19:46:01 +0200 (Mi, 01 Apr 2009) | 1 line
#5655: fix docstring oversight.
........
r70998 | georg.brandl | 2009-04-01 23:54:21 +0200 (Mi, 01 Apr 2009) | 1 line
In Pdb, stop assigning values to __builtin__._ which interferes with the one commonly installed by gettext.
........
r71056 | georg.brandl | 2009-04-02 19:43:07 +0200 (Do, 02 Apr 2009) | 2 lines
Actually the displayhook should print the repr.
........
2009-04-05 18:21:05 -03:00
|
|
|
accessible within the rest of the regular expression 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 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 below can also be referenced as the numbered group
|
|
|
|
``1``.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
For example, if the pattern is ``(?P<id>[a-zA-Z_]\w*)``, the group can be
|
|
|
|
referenced by its name in arguments to methods of match objects, such as
|
Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70866 | georg.brandl | 2009-03-31 21:06:57 +0200 (Di, 31 Mär 2009) | 1 line
#4882: document named group behavior a bit better.
........
r70867 | georg.brandl | 2009-03-31 21:10:35 +0200 (Di, 31 Mär 2009) | 1 line
#1096310: document usage of sys.__std*__ a bit better.
........
r70868 | georg.brandl | 2009-03-31 21:12:17 +0200 (Di, 31 Mär 2009) | 1 line
#5190: export make_option in __all__.
........
r70870 | georg.brandl | 2009-03-31 21:26:24 +0200 (Di, 31 Mär 2009) | 1 line
#4411: document mro() and __mro__. (I hope I got it right.)
........
r70871 | georg.brandl | 2009-03-31 21:30:56 +0200 (Di, 31 Mär 2009) | 1 line
#5618: fix typo.
........
r70893 | georg.brandl | 2009-03-31 22:56:32 +0200 (Di, 31 Mär 2009) | 1 line
#1530012: move TQS section before raw strings.
........
r70896 | georg.brandl | 2009-03-31 23:15:33 +0200 (Di, 31 Mär 2009) | 1 line
#5598: document DocFileSuite *args argument.
........
r70902 | georg.brandl | 2009-03-31 23:43:03 +0200 (Di, 31 Mär 2009) | 1 line
#1675026: add a note about a strange Windows problem, and remove notes about AtheOS.
........
r70905 | georg.brandl | 2009-04-01 00:03:40 +0200 (Mi, 01 Apr 2009) | 1 line
#5563: more documentation for bdist_msi.
........
r70907 | georg.brandl | 2009-04-01 00:18:19 +0200 (Mi, 01 Apr 2009) | 1 line
#3427: document correct return type for urlopen().info().
........
r70912 | georg.brandl | 2009-04-01 00:35:46 +0200 (Mi, 01 Apr 2009) | 1 line
#5617: add a handy function to print a unicode string to gdbinit.
........
r70915 | georg.brandl | 2009-04-01 00:40:16 +0200 (Mi, 01 Apr 2009) | 1 line
#5018: remove confusing paragraph.
........
r70927 | georg.brandl | 2009-04-01 01:01:27 +0200 (Mi, 01 Apr 2009) | 1 line
Dont shout to users.
........
r70933 | georg.brandl | 2009-04-01 02:04:33 +0200 (Mi, 01 Apr 2009) | 2 lines
Issue #5635: Fix running test_sys with tracing enabled.
........
r70940 | georg.brandl | 2009-04-01 06:21:14 +0200 (Mi, 01 Apr 2009) | 2 lines
The SimpleXMLRPCServer's CGI handler now runs like a pony.
........
r70944 | georg.brandl | 2009-04-01 06:32:39 +0200 (Mi, 01 Apr 2009) | 1 line
#5631: add upload to list of possible commands, which is presented in --help-commands.
........
r70954 | georg.brandl | 2009-04-01 17:23:43 +0200 (Mi, 01 Apr 2009) | 1 line
Fix test_xmlrpc and make the CGI handler work with no CONTENT_LENGTH.
........
r70963 | georg.brandl | 2009-04-01 19:46:01 +0200 (Mi, 01 Apr 2009) | 1 line
#5655: fix docstring oversight.
........
r70998 | georg.brandl | 2009-04-01 23:54:21 +0200 (Mi, 01 Apr 2009) | 1 line
In Pdb, stop assigning values to __builtin__._ which interferes with the one commonly installed by gettext.
........
r71056 | georg.brandl | 2009-04-02 19:43:07 +0200 (Do, 02 Apr 2009) | 2 lines
Actually the displayhook should print the repr.
........
2009-04-05 18:21:05 -03:00
|
|
|
``m.group('id')`` or ``m.end('id')``, and also by name in the regular
|
|
|
|
expression itself (using ``(?P=id)``) and replacement text given to
|
|
|
|
``.sub()`` (using ``\g<id>``).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
``(?P=name)``
|
|
|
|
Matches whatever text was matched by the earlier group named *name*.
|
|
|
|
|
|
|
|
``(?#...)``
|
|
|
|
A comment; the contents of the parentheses are simply ignored.
|
|
|
|
|
|
|
|
``(?=...)``
|
|
|
|
Matches if ``...`` matches next, but doesn't consume any of the string. This is
|
|
|
|
called a lookahead assertion. For example, ``Isaac (?=Asimov)`` will match
|
|
|
|
``'Isaac '`` only if it's followed by ``'Asimov'``.
|
|
|
|
|
|
|
|
``(?!...)``
|
|
|
|
Matches if ``...`` doesn't match next. This is a negative lookahead assertion.
|
|
|
|
For example, ``Isaac (?!Asimov)`` will match ``'Isaac '`` only if it's *not*
|
|
|
|
followed by ``'Asimov'``.
|
|
|
|
|
|
|
|
``(?<=...)``
|
|
|
|
Matches if the current position in the string is preceded by a match for ``...``
|
|
|
|
that ends at the current position. This is called a :dfn:`positive lookbehind
|
|
|
|
assertion`. ``(?<=abc)def`` will find a match in ``abcdef``, since the
|
|
|
|
lookbehind will back up 3 characters and check if the contained pattern matches.
|
|
|
|
The contained pattern must only match strings of some fixed length, meaning that
|
|
|
|
``abc`` or ``a|b`` are allowed, but ``a*`` and ``a{3,4}`` are not. Note that
|
|
|
|
patterns which start with positive lookbehind assertions will never match at the
|
|
|
|
beginning of the string being searched; you will most likely want to use the
|
2008-03-22 09:04:26 -03:00
|
|
|
:func:`search` function rather than the :func:`match` function:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> import re
|
|
|
|
>>> m = re.search('(?<=abc)def', 'abcdef')
|
|
|
|
>>> m.group(0)
|
|
|
|
'def'
|
|
|
|
|
2008-03-22 09:04:26 -03:00
|
|
|
This example looks for a word following a hyphen:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> m = re.search('(?<=-)\w+', 'spam-egg')
|
|
|
|
>>> m.group(0)
|
|
|
|
'egg'
|
|
|
|
|
|
|
|
``(?<!...)``
|
|
|
|
Matches if the current position in the string is not preceded by a match for
|
|
|
|
``...``. This is called a :dfn:`negative lookbehind assertion`. Similar to
|
|
|
|
positive lookbehind assertions, the contained pattern must only match strings of
|
|
|
|
some fixed length. Patterns which start with negative lookbehind assertions may
|
|
|
|
match at the beginning of the string being searched.
|
|
|
|
|
|
|
|
``(?(id/name)yes-pattern|no-pattern)``
|
|
|
|
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
|
|
|
|
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
|
|
|
|
``'user@host.com'``, but not with ``'<user@host.com'``.
|
|
|
|
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
|
|
|
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 ``'$'``.
|
|
|
|
|
|
|
|
``\number``
|
|
|
|
Matches the contents of the group of the same number. Groups are numbered
|
|
|
|
starting from 1. For example, ``(.+) \1`` matches ``'the the'`` or ``'55 55'``,
|
|
|
|
but not ``'the end'`` (note the space after the group). This special sequence
|
|
|
|
can only be used to match one of the first 99 groups. If the first digit of
|
|
|
|
*number* is 0, or *number* is 3 octal digits long, it will not be interpreted as
|
|
|
|
a group match, but as the character with octal value *number*. Inside the
|
|
|
|
``'['`` and ``']'`` of a character class, all numeric escapes are treated as
|
|
|
|
characters.
|
|
|
|
|
|
|
|
``\A``
|
|
|
|
Matches only at the start of the string.
|
|
|
|
|
|
|
|
``\b``
|
|
|
|
Matches the empty string, but only at the beginning or end of a word. A word is
|
|
|
|
defined as a sequence of alphanumeric or underscore characters, so the end of a
|
|
|
|
word is indicated by whitespace or a non-alphanumeric, non-underscore character.
|
2010-05-09 11:07:57 -03:00
|
|
|
Note that ``\b`` is defined as the boundary between ``\w`` and ``\W``, so the
|
2007-08-15 11:28:01 -03:00
|
|
|
precise set of characters deemed to be alphanumeric depends on the values of the
|
|
|
|
``UNICODE`` and ``LOCALE`` flags. Inside a character range, ``\b`` represents
|
|
|
|
the backspace character, for compatibility with Python's string literals.
|
|
|
|
|
|
|
|
``\B``
|
|
|
|
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
|
|
|
|
of ``LOCALE`` and ``UNICODE``.
|
|
|
|
|
|
|
|
``\d``
|
|
|
|
When the :const:`UNICODE` flag is not specified, matches any decimal digit; this
|
|
|
|
is equivalent to the set ``[0-9]``. With :const:`UNICODE`, it will match
|
|
|
|
whatever is classified as a digit in the Unicode character properties database.
|
|
|
|
|
|
|
|
``\D``
|
|
|
|
When the :const:`UNICODE` flag is not specified, matches any non-digit
|
|
|
|
character; this is equivalent to the set ``[^0-9]``. With :const:`UNICODE`, it
|
|
|
|
will match anything other than character marked as digits in the Unicode
|
|
|
|
character properties database.
|
|
|
|
|
|
|
|
``\s``
|
|
|
|
When the :const:`LOCALE` and :const:`UNICODE` flags are not specified, matches
|
|
|
|
any whitespace character; this is equivalent to the set ``[ \t\n\r\f\v]``. With
|
|
|
|
:const:`LOCALE`, it will match this set plus whatever characters are defined as
|
|
|
|
space for the current locale. If :const:`UNICODE` is set, this will match the
|
|
|
|
characters ``[ \t\n\r\f\v]`` plus whatever is classified as space in the Unicode
|
|
|
|
character properties database.
|
|
|
|
|
|
|
|
``\S``
|
|
|
|
When the :const:`LOCALE` and :const:`UNICODE` flags are not specified, matches
|
|
|
|
any non-whitespace character; this is equivalent to the set ``[^ \t\n\r\f\v]``
|
|
|
|
With :const:`LOCALE`, it will match any character not in this set, and not
|
|
|
|
defined as space in the current locale. If :const:`UNICODE` is set, this will
|
|
|
|
match anything other than ``[ \t\n\r\f\v]`` and characters marked as space in
|
|
|
|
the Unicode character properties database.
|
|
|
|
|
|
|
|
``\w``
|
|
|
|
When the :const:`LOCALE` and :const:`UNICODE` flags are not specified, matches
|
|
|
|
any alphanumeric character and the underscore; this is equivalent to the set
|
|
|
|
``[a-zA-Z0-9_]``. With :const:`LOCALE`, it will match the set ``[0-9_]`` plus
|
|
|
|
whatever characters are defined as alphanumeric for the current locale. If
|
|
|
|
:const:`UNICODE` is set, this will match the characters ``[0-9_]`` plus whatever
|
|
|
|
is classified as alphanumeric in the Unicode character properties database.
|
|
|
|
|
|
|
|
``\W``
|
|
|
|
When the :const:`LOCALE` and :const:`UNICODE` flags are not specified, matches
|
|
|
|
any non-alphanumeric character; this is equivalent to the set ``[^a-zA-Z0-9_]``.
|
|
|
|
With :const:`LOCALE`, it will match any character not in the set ``[0-9_]``, and
|
|
|
|
not defined as alphanumeric for the current locale. If :const:`UNICODE` is set,
|
|
|
|
this will match anything other than ``[0-9_]`` and characters marked as
|
|
|
|
alphanumeric in the Unicode character properties database.
|
|
|
|
|
|
|
|
``\Z``
|
|
|
|
Matches only at the end of the string.
|
|
|
|
|
|
|
|
Most of the standard escapes supported by Python string literals are also
|
|
|
|
accepted by the regular expression parser::
|
|
|
|
|
|
|
|
\a \b \f \n
|
|
|
|
\r \t \v \x
|
|
|
|
\\
|
|
|
|
|
|
|
|
Octal escapes are included in a limited form: If the first digit is a 0, or if
|
|
|
|
there are three octal digits, it is considered an octal escape. Otherwise, it is
|
|
|
|
a group reference. As for string literals, octal escapes are always at most
|
|
|
|
three digits in length.
|
|
|
|
|
|
|
|
|
|
|
|
.. _matching-searching:
|
|
|
|
|
|
|
|
Matching vs Searching
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
|
|
|
|
|
|
|
|
|
|
|
Python offers two different primitive operations based on regular expressions:
|
2007-08-23 18:36:05 -03:00
|
|
|
**match** checks for a match only at the beginning of the string, while
|
|
|
|
**search** checks for a match anywhere in the string (this is what Perl does
|
|
|
|
by default).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-08-23 18:36:05 -03:00
|
|
|
Note that match may differ from search even when using a regular expression
|
|
|
|
beginning with ``'^'``: ``'^'`` matches only at the start of the string, or in
|
2007-08-15 11:28:01 -03:00
|
|
|
:const:`MULTILINE` mode also immediately following a newline. The "match"
|
|
|
|
operation succeeds only if the pattern matches at the start of the string
|
|
|
|
regardless of mode, or at the starting position given by the optional *pos*
|
2008-03-22 09:04:26 -03:00
|
|
|
argument regardless of whether a newline precedes it.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-03-22 09:04:26 -03:00
|
|
|
>>> re.match("c", "abcdef") # No match
|
|
|
|
>>> re.search("c", "abcdef") # Match
|
|
|
|
<_sre.SRE_Match object at ...>
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _contents-of-module-re:
|
|
|
|
|
|
|
|
Module Contents
|
|
|
|
---------------
|
|
|
|
|
|
|
|
The module defines several functions, constants, and an exception. Some of the
|
|
|
|
functions are simplified versions of the full featured methods for compiled
|
|
|
|
regular expressions. Most non-trivial applications always use the compiled
|
|
|
|
form.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: compile(pattern[, flags])
|
|
|
|
|
2007-09-27 03:26:58 -03:00
|
|
|
Compile a regular expression pattern into a regular expression object, which
|
|
|
|
can be used for matching using its :func:`match` and :func:`search` methods,
|
2007-08-15 11:28:01 -03:00
|
|
|
described below.
|
|
|
|
|
|
|
|
The expression's behaviour can be modified by specifying a *flags* value.
|
|
|
|
Values can be any of the following variables, combined using bitwise OR (the
|
|
|
|
``|`` operator).
|
|
|
|
|
|
|
|
The sequence ::
|
|
|
|
|
2009-03-02 01:25:11 -04:00
|
|
|
prog = re.compile(pattern)
|
|
|
|
result = prog.match(string)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
is equivalent to ::
|
|
|
|
|
2009-03-02 01:25:11 -04:00
|
|
|
result = re.match(pattern, string)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
but using :func:`re.compile` and saving the resulting regular expression
|
|
|
|
object for reuse is more efficient when the expression will be used several
|
|
|
|
times in a single program.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-03-02 01:25:11 -04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The compiled versions of the most recent patterns passed to
|
|
|
|
:func:`re.match`, :func:`re.search` or :func:`re.compile` are cached, so
|
|
|
|
programs that use only a few regular expressions at a time needn't worry
|
|
|
|
about compiling regular expressions.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: I
|
|
|
|
IGNORECASE
|
|
|
|
|
|
|
|
Perform case-insensitive matching; expressions like ``[A-Z]`` will match
|
|
|
|
lowercase letters, too. This is not affected by the current locale.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: L
|
|
|
|
LOCALE
|
|
|
|
|
2007-09-27 03:26:58 -03:00
|
|
|
Make ``\w``, ``\W``, ``\b``, ``\B``, ``\s`` and ``\S`` dependent on the
|
|
|
|
current locale.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: M
|
|
|
|
MULTILINE
|
|
|
|
|
|
|
|
When specified, the pattern character ``'^'`` matches at the beginning of the
|
|
|
|
string and at the beginning of each line (immediately following each newline);
|
|
|
|
and the pattern character ``'$'`` matches at the end of the string and at the
|
|
|
|
end of each line (immediately preceding each newline). By default, ``'^'``
|
|
|
|
matches only at the beginning of the string, and ``'$'`` only at the end of the
|
|
|
|
string and immediately before the newline (if any) at the end of the string.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: S
|
|
|
|
DOTALL
|
|
|
|
|
|
|
|
Make the ``'.'`` special character match any character at all, including a
|
|
|
|
newline; without this flag, ``'.'`` will match anything *except* a newline.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: U
|
|
|
|
UNICODE
|
|
|
|
|
|
|
|
Make ``\w``, ``\W``, ``\b``, ``\B``, ``\d``, ``\D``, ``\s`` and ``\S`` dependent
|
|
|
|
on the Unicode character properties database.
|
|
|
|
|
|
|
|
.. versionadded:: 2.0
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: X
|
|
|
|
VERBOSE
|
|
|
|
|
|
|
|
This flag allows you to write regular expressions that look nicer. Whitespace
|
|
|
|
within the pattern is ignored, except when in a character class or preceded by
|
|
|
|
an unescaped backslash, and, when a line contains a ``'#'`` neither in a
|
|
|
|
character class or preceded by an unescaped backslash, all characters from the
|
|
|
|
leftmost such ``'#'`` through the end of the line are ignored.
|
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
That means that the two following regular expression objects that match a
|
|
|
|
decimal number are functionally equal::
|
|
|
|
|
|
|
|
a = re.compile(r"""\d + # the integral part
|
|
|
|
\. # the decimal point
|
|
|
|
\d * # some fractional digits""", re.X)
|
|
|
|
b = re.compile(r"\d+\.\d*")
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: search(pattern, string[, flags])
|
|
|
|
|
|
|
|
Scan through *string* looking for a location where the regular expression
|
|
|
|
*pattern* produces a match, and return a corresponding :class:`MatchObject`
|
|
|
|
instance. Return ``None`` if no position in the string matches the pattern; note
|
|
|
|
that this is different from finding a zero-length match at some point in the
|
|
|
|
string.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: match(pattern, string[, flags])
|
|
|
|
|
|
|
|
If zero or more characters at the beginning of *string* match the regular
|
|
|
|
expression *pattern*, return a corresponding :class:`MatchObject` instance.
|
|
|
|
Return ``None`` if the string does not match the pattern; note that this is
|
|
|
|
different from a zero-length match.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
If you want to locate a match anywhere in *string*, use :func:`search`
|
2007-12-05 14:30:48 -04:00
|
|
|
instead.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: split(pattern, string[, maxsplit=0])
|
|
|
|
|
|
|
|
Split *string* by the occurrences of *pattern*. If capturing parentheses are
|
|
|
|
used in *pattern*, then the text of all groups in the pattern are also returned
|
|
|
|
as part of the resulting list. If *maxsplit* is nonzero, at most *maxsplit*
|
|
|
|
splits occur, and the remainder of the string is returned as the final element
|
|
|
|
of the list. (Incompatibility note: in the original Python 1.5 release,
|
2008-03-22 09:04:26 -03:00
|
|
|
*maxsplit* was ignored. This has been fixed in later releases.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> re.split('\W+', 'Words, words, words.')
|
|
|
|
['Words', 'words', 'words', '']
|
|
|
|
>>> re.split('(\W+)', 'Words, words, words.')
|
|
|
|
['Words', ', ', 'words', ', ', 'words', '.', '']
|
|
|
|
>>> re.split('\W+', 'Words, words, words.', 1)
|
|
|
|
['Words', 'words, words.']
|
|
|
|
|
2008-03-06 03:19:15 -04:00
|
|
|
If there are capturing groups in the separator and it matches at the start of
|
|
|
|
the string, the result will start with an empty string. The same holds for
|
2008-03-22 09:04:26 -03:00
|
|
|
the end of the string:
|
2008-03-06 03:19:15 -04:00
|
|
|
|
|
|
|
>>> re.split('(\W+)', '...words, words...')
|
|
|
|
['', '...', 'words', ', ', 'words', '...', '']
|
|
|
|
|
|
|
|
That way, separator components are always found at the same relative
|
|
|
|
indices within the result list (e.g., if there's one capturing group
|
|
|
|
in the separator, the 0th, the 2nd and so forth).
|
|
|
|
|
2007-09-01 14:40:03 -03:00
|
|
|
Note that *split* will never split a string on an empty pattern match.
|
2008-03-22 09:04:26 -03:00
|
|
|
For example:
|
2007-09-01 14:40:03 -03:00
|
|
|
|
|
|
|
>>> re.split('x*', 'foo')
|
|
|
|
['foo']
|
|
|
|
>>> re.split("(?m)^$", "foo\n\nbar\n")
|
|
|
|
['foo\n\nbar\n']
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2008-03-06 03:19:15 -04:00
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. function:: findall(pattern, string[, flags])
|
|
|
|
|
2007-09-27 03:26:58 -03:00
|
|
|
Return all non-overlapping matches of *pattern* in *string*, as a list of
|
2008-07-19 10:48:44 -03:00
|
|
|
strings. The *string* is scanned left-to-right, and matches are returned in
|
|
|
|
the order found. If one or more groups are present in the pattern, return a
|
|
|
|
list of groups; this will be a list of tuples if the pattern has more than
|
|
|
|
one group. Empty matches are included in the result unless they touch the
|
|
|
|
beginning of another match.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 1.5.2
|
|
|
|
|
|
|
|
.. versionchanged:: 2.4
|
|
|
|
Added the optional flags argument.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: finditer(pattern, string[, flags])
|
|
|
|
|
2007-10-21 09:10:28 -03:00
|
|
|
Return an :term:`iterator` yielding :class:`MatchObject` instances over all
|
2008-07-19 10:48:44 -03:00
|
|
|
non-overlapping matches for the RE *pattern* in *string*. The *string* is
|
|
|
|
scanned left-to-right, and matches are returned in the order found. Empty
|
|
|
|
matches are included in the result unless they touch the beginning of another
|
|
|
|
match.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|
|
|
|
.. versionchanged:: 2.4
|
|
|
|
Added the optional flags argument.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: sub(pattern, repl, string[, count])
|
|
|
|
|
|
|
|
Return the string obtained by replacing the leftmost non-overlapping occurrences
|
|
|
|
of *pattern* in *string* by the replacement *repl*. If the pattern isn't found,
|
|
|
|
*string* is returned unchanged. *repl* can be a string or a function; if it is
|
|
|
|
a string, any backslash escapes in it are processed. That is, ``\n`` is
|
|
|
|
converted to a single newline character, ``\r`` is converted to a linefeed, and
|
|
|
|
so forth. Unknown escapes such as ``\j`` are left alone. Backreferences, such
|
|
|
|
as ``\6``, are replaced with the substring matched by group 6 in the pattern.
|
2008-03-22 09:04:26 -03:00
|
|
|
For example:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
|
|
|
|
... r'static PyObject*\npy_\1(void)\n{',
|
|
|
|
... 'def myfunc():')
|
|
|
|
'static PyObject*\npy_myfunc(void)\n{'
|
|
|
|
|
|
|
|
If *repl* is a function, it is called for every non-overlapping occurrence of
|
|
|
|
*pattern*. The function takes a single match object argument, and returns the
|
2008-03-22 09:04:26 -03:00
|
|
|
replacement string. For example:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> def dashrepl(matchobj):
|
|
|
|
... if matchobj.group(0) == '-': return ' '
|
|
|
|
... else: return '-'
|
|
|
|
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
|
|
|
|
'pro--gram files'
|
|
|
|
|
|
|
|
The pattern may be a string or an RE object; if you need to specify regular
|
|
|
|
expression flags, you must use a RE object, or use embedded modifiers in a
|
|
|
|
pattern; for example, ``sub("(?i)b+", "x", "bbbb BBBB")`` returns ``'x x'``.
|
|
|
|
|
|
|
|
The optional argument *count* is the maximum number of pattern occurrences to be
|
|
|
|
replaced; *count* must be a non-negative integer. If omitted or zero, all
|
|
|
|
occurrences will be replaced. Empty matches for the pattern are replaced only
|
|
|
|
when not adjacent to a previous match, so ``sub('x*', '-', 'abc')`` returns
|
|
|
|
``'-a-b-c-'``.
|
|
|
|
|
|
|
|
In addition to character escapes and backreferences as described above,
|
|
|
|
``\g<name>`` will use the substring matched by the group named ``name``, as
|
|
|
|
defined by the ``(?P<name>...)`` syntax. ``\g<number>`` uses the corresponding
|
|
|
|
group number; ``\g<2>`` is therefore equivalent to ``\2``, but isn't ambiguous
|
|
|
|
in a replacement such as ``\g<2>0``. ``\20`` would be interpreted as a
|
|
|
|
reference to group 20, not a reference to group 2 followed by the literal
|
|
|
|
character ``'0'``. The backreference ``\g<0>`` substitutes in the entire
|
|
|
|
substring matched by the RE.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: subn(pattern, repl, string[, count])
|
|
|
|
|
|
|
|
Perform the same operation as :func:`sub`, but return a tuple ``(new_string,
|
|
|
|
number_of_subs_made)``.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: escape(string)
|
|
|
|
|
|
|
|
Return *string* with all non-alphanumerics backslashed; this is useful if you
|
|
|
|
want to match an arbitrary literal string that may have regular expression
|
|
|
|
metacharacters in it.
|
|
|
|
|
|
|
|
|
2010-07-10 11:27:35 -03:00
|
|
|
.. function:: purge()
|
|
|
|
|
|
|
|
Clear the regular expression cache.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. exception:: error
|
|
|
|
|
|
|
|
Exception raised when a string passed to one of the functions here is not a
|
|
|
|
valid regular expression (for example, it might contain unmatched parentheses)
|
|
|
|
or when some other error occurs during compilation or matching. It is never an
|
|
|
|
error if a string contains no match for a pattern.
|
|
|
|
|
|
|
|
|
|
|
|
.. _re-objects:
|
|
|
|
|
|
|
|
Regular Expression Objects
|
|
|
|
--------------------------
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. class:: RegexObject
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The :class:`RegexObject` class supports the following methods and attributes:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-06-01 20:21:12 -03:00
|
|
|
.. method:: RegexObject.search(string[, pos[, endpos]])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-06-01 20:21:12 -03:00
|
|
|
Scan through *string* looking for a location where this regular expression
|
|
|
|
produces a match, and return a corresponding :class:`MatchObject` instance.
|
|
|
|
Return ``None`` if no position in the string matches the pattern; note that this
|
|
|
|
is different from finding a zero-length match at some point in the string.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The optional second parameter *pos* gives an index in the string where the
|
|
|
|
search is to start; it defaults to ``0``. This is not completely equivalent to
|
|
|
|
slicing the string; the ``'^'`` pattern character matches at the real beginning
|
|
|
|
of the string and at positions just after a newline, but not necessarily at the
|
|
|
|
index where the search is to start.
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The optional parameter *endpos* limits how far the string will be searched; it
|
|
|
|
will be as if the string is *endpos* characters long, so only the characters
|
|
|
|
from *pos* to ``endpos - 1`` will be searched for a match. If *endpos* is less
|
|
|
|
than *pos*, no match will be found, otherwise, if *rx* is a compiled regular
|
2010-06-01 20:21:12 -03:00
|
|
|
expression object, ``rx.search(string, 0, 50)`` is equivalent to
|
|
|
|
``rx.search(string[:50], 0)``.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-06-01 20:21:12 -03:00
|
|
|
>>> pattern = re.compile("d")
|
|
|
|
>>> pattern.search("dog") # Match at index 0
|
|
|
|
<_sre.SRE_Match object at ...>
|
|
|
|
>>> pattern.search("dog", 1) # No match; search doesn't include the "d"
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-06-01 20:21:12 -03:00
|
|
|
.. method:: RegexObject.match(string[, pos[, endpos]])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-06-01 20:21:12 -03:00
|
|
|
If zero or more characters at the *beginning* of *string* match this regular
|
|
|
|
expression, return a corresponding :class:`MatchObject` instance. Return
|
|
|
|
``None`` if the string does not match the pattern; note that this is different
|
|
|
|
from a zero-length match.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The optional *pos* and *endpos* parameters have the same meaning as for the
|
2010-06-01 20:21:12 -03:00
|
|
|
:meth:`~RegexObject.search` method.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you want to locate a match anywhere in *string*, use
|
|
|
|
:meth:`~RegexObject.search` instead.
|
|
|
|
|
|
|
|
>>> pattern = re.compile("o")
|
|
|
|
>>> pattern.match("dog") # No match as "o" is not at the start of "dog".
|
|
|
|
>>> pattern.match("dog", 1) # Match as "o" is the 2nd character of "dog".
|
|
|
|
<_sre.SRE_Match object at ...>
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: RegexObject.split(string[, maxsplit=0])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Identical to the :func:`split` function, using the compiled pattern.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: RegexObject.findall(string[, pos[, endpos]])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-05-22 05:21:45 -03:00
|
|
|
Similar to the :func:`findall` function, using the compiled pattern, but
|
|
|
|
also accepts optional *pos* and *endpos* parameters that limit the search
|
|
|
|
region like for :meth:`match`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: RegexObject.finditer(string[, pos[, endpos]])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-05-22 05:21:45 -03:00
|
|
|
Similar to the :func:`finditer` function, using the compiled pattern, but
|
|
|
|
also accepts optional *pos* and *endpos* parameters that limit the search
|
|
|
|
region like for :meth:`match`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: RegexObject.sub(repl, string[, count=0])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Identical to the :func:`sub` function, using the compiled pattern.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: RegexObject.subn(repl, string[, count=0])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Identical to the :func:`subn` function, using the compiled pattern.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: RegexObject.flags
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The flags argument used when the RE object was compiled, or ``0`` if no flags
|
|
|
|
were provided.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
Merged revisions 67326,67498,67531-67532,67538,67553-67554,67556-67557 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67326 | benjamin.peterson | 2008-11-22 02:59:15 +0100 (Sat, 22 Nov 2008) | 1 line
backport r67325: make FileIO.mode always contain 'b'
........
r67498 | raymond.hettinger | 2008-12-03 16:42:10 +0100 (Wed, 03 Dec 2008) | 1 line
Backport r67478
........
r67531 | georg.brandl | 2008-12-04 19:54:05 +0100 (Thu, 04 Dec 2008) | 2 lines
Add reference to enumerate() to indices example.
........
r67532 | georg.brandl | 2008-12-04 19:59:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Add another heapq example.
........
r67538 | georg.brandl | 2008-12-04 22:28:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Clarification to avoid confusing output with file descriptors.
........
r67553 | georg.brandl | 2008-12-05 08:49:49 +0100 (Fri, 05 Dec 2008) | 2 lines
#4408: document regex.groups.
........
r67554 | georg.brandl | 2008-12-05 08:52:26 +0100 (Fri, 05 Dec 2008) | 2 lines
#4409: fix asterisks looking like footnotes.
........
r67556 | georg.brandl | 2008-12-05 09:02:17 +0100 (Fri, 05 Dec 2008) | 2 lines
#4441: improve doc for os.open() flags.
........
r67557 | georg.brandl | 2008-12-05 09:06:57 +0100 (Fri, 05 Dec 2008) | 2 lines
Add an index entry for "subclassing immutable types".
........
2008-12-05 05:08:28 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: RegexObject.groups
|
Merged revisions 67326,67498,67531-67532,67538,67553-67554,67556-67557 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67326 | benjamin.peterson | 2008-11-22 02:59:15 +0100 (Sat, 22 Nov 2008) | 1 line
backport r67325: make FileIO.mode always contain 'b'
........
r67498 | raymond.hettinger | 2008-12-03 16:42:10 +0100 (Wed, 03 Dec 2008) | 1 line
Backport r67478
........
r67531 | georg.brandl | 2008-12-04 19:54:05 +0100 (Thu, 04 Dec 2008) | 2 lines
Add reference to enumerate() to indices example.
........
r67532 | georg.brandl | 2008-12-04 19:59:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Add another heapq example.
........
r67538 | georg.brandl | 2008-12-04 22:28:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Clarification to avoid confusing output with file descriptors.
........
r67553 | georg.brandl | 2008-12-05 08:49:49 +0100 (Fri, 05 Dec 2008) | 2 lines
#4408: document regex.groups.
........
r67554 | georg.brandl | 2008-12-05 08:52:26 +0100 (Fri, 05 Dec 2008) | 2 lines
#4409: fix asterisks looking like footnotes.
........
r67556 | georg.brandl | 2008-12-05 09:02:17 +0100 (Fri, 05 Dec 2008) | 2 lines
#4441: improve doc for os.open() flags.
........
r67557 | georg.brandl | 2008-12-05 09:06:57 +0100 (Fri, 05 Dec 2008) | 2 lines
Add an index entry for "subclassing immutable types".
........
2008-12-05 05:08:28 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The number of capturing groups in the pattern.
|
Merged revisions 67326,67498,67531-67532,67538,67553-67554,67556-67557 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67326 | benjamin.peterson | 2008-11-22 02:59:15 +0100 (Sat, 22 Nov 2008) | 1 line
backport r67325: make FileIO.mode always contain 'b'
........
r67498 | raymond.hettinger | 2008-12-03 16:42:10 +0100 (Wed, 03 Dec 2008) | 1 line
Backport r67478
........
r67531 | georg.brandl | 2008-12-04 19:54:05 +0100 (Thu, 04 Dec 2008) | 2 lines
Add reference to enumerate() to indices example.
........
r67532 | georg.brandl | 2008-12-04 19:59:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Add another heapq example.
........
r67538 | georg.brandl | 2008-12-04 22:28:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Clarification to avoid confusing output with file descriptors.
........
r67553 | georg.brandl | 2008-12-05 08:49:49 +0100 (Fri, 05 Dec 2008) | 2 lines
#4408: document regex.groups.
........
r67554 | georg.brandl | 2008-12-05 08:52:26 +0100 (Fri, 05 Dec 2008) | 2 lines
#4409: fix asterisks looking like footnotes.
........
r67556 | georg.brandl | 2008-12-05 09:02:17 +0100 (Fri, 05 Dec 2008) | 2 lines
#4441: improve doc for os.open() flags.
........
r67557 | georg.brandl | 2008-12-05 09:06:57 +0100 (Fri, 05 Dec 2008) | 2 lines
Add an index entry for "subclassing immutable types".
........
2008-12-05 05:08:28 -04:00
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: RegexObject.groupindex
|
|
|
|
|
|
|
|
A dictionary mapping any symbolic group names defined by ``(?P<id>)`` to group
|
|
|
|
numbers. The dictionary is empty if no symbolic groups were used in the
|
|
|
|
pattern.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: RegexObject.pattern
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The pattern string from which the RE object was compiled.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _match-objects:
|
|
|
|
|
|
|
|
Match Objects
|
|
|
|
-------------
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. class:: MatchObject
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Match Objects always have a boolean value of :const:`True`, so that you can test
|
|
|
|
whether e.g. :func:`match` resulted in a match with a simple if statement. They
|
|
|
|
support the following methods and attributes:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.expand(template)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Return the string obtained by doing backslash substitution on the template
|
|
|
|
string *template*, as done by the :meth:`~RegexObject.sub` method. Escapes
|
|
|
|
such as ``\n`` are converted to the appropriate characters, and numeric
|
|
|
|
backreferences (``\1``, ``\2``) and named backreferences (``\g<1>``,
|
|
|
|
``\g<name>``) are replaced by the contents of the corresponding group.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.group([group1, ...])
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Returns one or more subgroups of the match. If there is a single argument, the
|
|
|
|
result is a single string; if there are multiple arguments, the result is a
|
|
|
|
tuple with one item per argument. Without arguments, *group1* defaults to zero
|
|
|
|
(the whole match is returned). If a *groupN* argument is zero, the corresponding
|
|
|
|
return value is the entire matching string; if it is in the inclusive range
|
|
|
|
[1..99], it is the string matching the corresponding parenthesized group. If a
|
|
|
|
group number is negative or larger than the number of groups defined in the
|
|
|
|
pattern, an :exc:`IndexError` exception is raised. If a group is contained in a
|
|
|
|
part of the pattern that did not match, the corresponding result is ``None``.
|
|
|
|
If a group is contained in a part of the pattern that matched multiple times,
|
|
|
|
the last match is returned.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
|
|
|
|
>>> m.group(0) # The entire match
|
|
|
|
'Isaac Newton'
|
|
|
|
>>> m.group(1) # The first parenthesized subgroup.
|
|
|
|
'Isaac'
|
|
|
|
>>> m.group(2) # The second parenthesized subgroup.
|
|
|
|
'Newton'
|
|
|
|
>>> m.group(1, 2) # Multiple arguments give us a tuple.
|
|
|
|
('Isaac', 'Newton')
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
If the regular expression uses the ``(?P<name>...)`` syntax, the *groupN*
|
|
|
|
arguments may also be strings identifying groups by their group name. If a
|
|
|
|
string argument is not used as a group name in the pattern, an :exc:`IndexError`
|
|
|
|
exception is raised.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
A moderately complicated example:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
|
|
|
|
>>> m.group('first_name')
|
|
|
|
'Malcolm'
|
|
|
|
>>> m.group('last_name')
|
|
|
|
'Reynolds'
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Named groups can also be referred to by their index:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m.group(1)
|
|
|
|
'Malcolm'
|
|
|
|
>>> m.group(2)
|
|
|
|
'Reynolds'
|
2008-03-22 09:04:26 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
If a group matches multiple times, only the last match is accessible:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(..)+", "a1b2c3") # Matches 3 times.
|
|
|
|
>>> m.group(1) # Returns only the last match.
|
|
|
|
'c3'
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.groups([default])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Return a tuple containing all the subgroups of the match, from 1 up to however
|
|
|
|
many groups are in the pattern. The *default* argument is used for groups that
|
|
|
|
did not participate in the match; it defaults to ``None``. (Incompatibility
|
|
|
|
note: in the original Python 1.5 release, if the tuple was one element long, a
|
|
|
|
string would be returned instead. In later versions (from 1.5.1 on), a
|
|
|
|
singleton tuple is returned in such cases.)
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
For example:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
|
|
|
|
>>> m.groups()
|
|
|
|
('24', '1632')
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
If we make the decimal place and everything after it optional, not all groups
|
|
|
|
might participate in the match. These groups will default to ``None`` unless
|
|
|
|
the *default* argument is given:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(\d+)\.?(\d+)?", "24")
|
|
|
|
>>> m.groups() # Second group defaults to None.
|
|
|
|
('24', None)
|
|
|
|
>>> m.groups('0') # Now, the second group defaults to '0'.
|
|
|
|
('24', '0')
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.groupdict([default])
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Return a dictionary containing all the *named* subgroups of the match, keyed by
|
|
|
|
the subgroup name. The *default* argument is used for groups that did not
|
|
|
|
participate in the match; it defaults to ``None``. For example:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
|
|
|
|
>>> m.groupdict()
|
|
|
|
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.start([group])
|
|
|
|
MatchObject.end([group])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Return the indices of the start and end of the substring matched by *group*;
|
|
|
|
*group* defaults to zero (meaning the whole matched substring). Return ``-1`` if
|
|
|
|
*group* exists but did not contribute to the match. For a match object *m*, and
|
|
|
|
a group *g* that did contribute to the match, the substring matched by group *g*
|
|
|
|
(equivalent to ``m.group(g)``) is ::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
m.string[m.start(g):m.end(g)]
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
Note that ``m.start(group)`` will equal ``m.end(group)`` if *group* matched a
|
|
|
|
null string. For example, after ``m = re.search('b(c?)', 'cba')``,
|
|
|
|
``m.start(0)`` is 1, ``m.end(0)`` is 2, ``m.start(1)`` and ``m.end(1)`` are both
|
|
|
|
2, and ``m.start(2)`` raises an :exc:`IndexError` exception.
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
An example that will remove *remove_this* from email addresses:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
>>> email = "tony@tiremove_thisger.net"
|
|
|
|
>>> m = re.search("remove_this", email)
|
|
|
|
>>> email[:m.start()] + email[m.end():]
|
|
|
|
'tony@tiger.net'
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. method:: MatchObject.span([group])
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
For :class:`MatchObject` *m*, return the 2-tuple ``(m.start(group),
|
|
|
|
m.end(group))``. Note that if *group* did not contribute to the match, this is
|
|
|
|
``(-1, -1)``. *group* defaults to zero, the entire match.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.pos
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The value of *pos* which was passed to the :meth:`~RegexObject.search` or
|
|
|
|
:meth:`~RegexObject.match` method of the :class:`RegexObject`. This is the
|
|
|
|
index into the string at which the RE engine started looking for a match.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.endpos
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The value of *endpos* which was passed to the :meth:`~RegexObject.search` or
|
|
|
|
:meth:`~RegexObject.match` method of the :class:`RegexObject`. This is the
|
|
|
|
index into the string beyond which the RE engine will not go.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.lastindex
|
|
|
|
|
|
|
|
The integer index of the last matched capturing group, or ``None`` if no group
|
|
|
|
was matched at all. For example, the expressions ``(a)b``, ``((a)(b))``, and
|
|
|
|
``((ab))`` will have ``lastindex == 1`` if applied to the string ``'ab'``, while
|
|
|
|
the expression ``(a)(b)`` will have ``lastindex == 2``, if applied to the same
|
|
|
|
string.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.lastgroup
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The name of the last matched capturing group, or ``None`` if the group didn't
|
|
|
|
have a name, or if no group was matched at all.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.re
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The regular expression object whose :meth:`~RegexObject.match` or
|
|
|
|
:meth:`~RegexObject.search` method produced this :class:`MatchObject`
|
|
|
|
instance.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
.. attribute:: MatchObject.string
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2010-03-25 20:56:05 -03:00
|
|
|
The string passed to :meth:`~RegexObject.match` or
|
|
|
|
:meth:`~RegexObject.search`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
Checking For a Pair
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
In this example, we'll use the following helper function to display match
|
2008-03-22 09:04:26 -03:00
|
|
|
objects a little more gracefully:
|
|
|
|
|
2008-03-22 10:07:06 -03:00
|
|
|
.. testcode::
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
def displaymatch(match):
|
|
|
|
if match is None:
|
|
|
|
return None
|
|
|
|
return '<Match: %r, groups=%r>' % (match.group(), match.groups())
|
|
|
|
|
|
|
|
Suppose you are writing a poker program where a player's hand is represented as
|
|
|
|
a 5-character string with each character representing a card, "a" for ace, "k"
|
|
|
|
for king, "q" for queen, j for jack, "0" for 10, and "1" through "9"
|
|
|
|
representing the card with that value.
|
|
|
|
|
2008-03-22 09:04:26 -03:00
|
|
|
To see if a given string is a valid hand, one could do the following:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2008-03-22 09:04:26 -03:00
|
|
|
>>> valid = re.compile(r"[0-9akqj]{5}$")
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> displaymatch(valid.match("ak05q")) # Valid.
|
2008-03-22 09:04:26 -03:00
|
|
|
"<Match: 'ak05q', groups=()>"
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> displaymatch(valid.match("ak05e")) # Invalid.
|
|
|
|
>>> displaymatch(valid.match("ak0")) # Invalid.
|
|
|
|
>>> displaymatch(valid.match("727ak")) # Valid.
|
2008-03-22 09:04:26 -03:00
|
|
|
"<Match: '727ak', groups=()>"
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
That last hand, ``"727ak"``, contained a pair, or two of the same valued cards.
|
2008-03-22 09:04:26 -03:00
|
|
|
To match this with a regular expression, one could use backreferences as such:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> pair = re.compile(r".*(.).*\1")
|
|
|
|
>>> displaymatch(pair.match("717ak")) # Pair of 7s.
|
2008-03-22 09:04:26 -03:00
|
|
|
"<Match: '717', groups=('7',)>"
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> displaymatch(pair.match("718ak")) # No pairs.
|
|
|
|
>>> displaymatch(pair.match("354aa")) # Pair of aces.
|
2008-03-22 09:04:26 -03:00
|
|
|
"<Match: '354aa', groups=('a',)>"
|
2007-12-05 14:30:48 -04:00
|
|
|
|
Merged revisions 74008,74021-74022,74074-74075,74077,74148,74179,74188,74192-74194,74200,74205 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74008 | benjamin.peterson | 2009-07-15 02:46:42 +0200 (Mi, 15 Jul 2009) | 1 line
update year
........
r74021 | georg.brandl | 2009-07-16 09:33:04 +0200 (Do, 16 Jul 2009) | 1 line
#6486: start with built in functions rather than "built in objects".
........
r74022 | georg.brandl | 2009-07-16 09:38:35 +0200 (Do, 16 Jul 2009) | 1 line
#6481: fix typo in os.system() replacement.
........
r74074 | georg.brandl | 2009-07-18 11:03:10 +0200 (Sa, 18 Jul 2009) | 1 line
#6513: fix example code: warning categories are classes, not instances.
........
r74075 | georg.brandl | 2009-07-18 11:06:31 +0200 (Sa, 18 Jul 2009) | 1 line
#6505: fix typos.
........
r74077 | georg.brandl | 2009-07-18 11:43:40 +0200 (Sa, 18 Jul 2009) | 1 line
#6489: fix an ambiguity in getiterator() documentation.
........
r74148 | ezio.melotti | 2009-07-21 22:18:27 +0200 (Di, 21 Jul 2009) | 1 line
#6536 fixed typo
........
r74179 | ezio.melotti | 2009-07-22 23:08:49 +0200 (Mi, 22 Jul 2009) | 1 line
#6423 has_key -> in
........
r74188 | benjamin.peterson | 2009-07-23 16:25:31 +0200 (Do, 23 Jul 2009) | 1 line
use bools
........
r74192 | georg.brandl | 2009-07-24 18:28:38 +0200 (Fr, 24 Jul 2009) | 1 line
Fix arg types of et#.
........
r74193 | georg.brandl | 2009-07-24 18:46:38 +0200 (Fr, 24 Jul 2009) | 1 line
Dont put "void" in signature for nullary functions.
........
r74194 | georg.brandl | 2009-07-24 22:09:46 +0200 (Fr, 24 Jul 2009) | 1 line
#6564: fix section about the two raise syntaxes.
........
r74200 | georg.brandl | 2009-07-25 15:02:15 +0200 (Sa, 25 Jul 2009) | 1 line
#6571: add index entries for more operators.
........
r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line
#6576: fix cross-refs in re docs.
........
2009-10-27 11:34:21 -03:00
|
|
|
To find out what card the pair consists of, one could use the
|
|
|
|
:meth:`~MatchObject.group` method of :class:`MatchObject` in the following
|
|
|
|
manner:
|
2008-03-22 09:04:26 -03:00
|
|
|
|
2008-03-22 10:07:06 -03:00
|
|
|
.. doctest::
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> pair.match("717ak").group(1)
|
|
|
|
'7'
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
# Error because re.match() returns None, which doesn't have a group() method:
|
|
|
|
>>> pair.match("718ak").group(1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<pyshell#23>", line 1, in <module>
|
|
|
|
re.match(r".*(.).*\1", "718ak").group(1)
|
|
|
|
AttributeError: 'NoneType' object has no attribute 'group'
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> pair.match("354aa").group(1)
|
|
|
|
'a'
|
|
|
|
|
|
|
|
|
|
|
|
Simulating scanf()
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: single: scanf()
|
|
|
|
|
|
|
|
Python does not currently have an equivalent to :cfunc:`scanf`. Regular
|
|
|
|
expressions are generally more powerful, though also more verbose, than
|
|
|
|
:cfunc:`scanf` format strings. The table below offers some more-or-less
|
|
|
|
equivalent mappings between :cfunc:`scanf` format tokens and regular
|
|
|
|
expressions.
|
|
|
|
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| :cfunc:`scanf` Token | Regular Expression |
|
|
|
|
+================================+=============================================+
|
|
|
|
| ``%c`` | ``.`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%5c`` | ``.{5}`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%d`` | ``[-+]?\d+`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%e``, ``%E``, ``%f``, ``%g`` | ``[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%i`` | ``[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%o`` | ``0[0-7]*`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%s`` | ``\S+`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%u`` | ``\d+`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
| ``%x``, ``%X`` | ``0[xX][\dA-Fa-f]+`` |
|
|
|
|
+--------------------------------+---------------------------------------------+
|
|
|
|
|
|
|
|
To extract the filename and numbers from a string like ::
|
|
|
|
|
|
|
|
/usr/sbin/sendmail - 0 errors, 4 warnings
|
|
|
|
|
|
|
|
you would use a :cfunc:`scanf` format like ::
|
|
|
|
|
|
|
|
%s - %d errors, %d warnings
|
|
|
|
|
|
|
|
The equivalent regular expression would be ::
|
|
|
|
|
|
|
|
(\S+) - (\d+) errors, (\d+) warnings
|
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
Avoiding recursion
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
If you create regular expressions that require the engine to perform a lot of
|
|
|
|
recursion, you may encounter a :exc:`RuntimeError` exception with the message
|
|
|
|
``maximum recursion limit`` exceeded. For example, ::
|
|
|
|
|
|
|
|
>>> s = 'Begin ' + 1000*'a very long string ' + 'end'
|
|
|
|
>>> re.match('Begin (\w| )*? end', s).end()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
|
|
|
File "/usr/local/lib/python2.5/re.py", line 132, in match
|
|
|
|
return _compile(pattern, flags).match(string)
|
|
|
|
RuntimeError: maximum recursion limit exceeded
|
|
|
|
|
|
|
|
You can often restructure your regular expression to avoid recursion.
|
|
|
|
|
|
|
|
Starting with Python 2.3, simple uses of the ``*?`` pattern are special-cased to
|
|
|
|
avoid recursion. Thus, the above regular expression can avoid recursion by
|
|
|
|
being recast as ``Begin [a-zA-Z0-9_ ]*?end``. As a further benefit, such
|
|
|
|
regular expressions will run faster than their recursive equivalents.
|
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
search() vs. match()
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
In a nutshell, :func:`match` only attempts to match a pattern at the beginning
|
|
|
|
of a string where :func:`search` will match a pattern anywhere in a string.
|
2008-03-22 09:04:26 -03:00
|
|
|
For example:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> re.match("o", "dog") # No match as "o" is not the first letter of "dog".
|
|
|
|
>>> re.search("o", "dog") # Match as search() looks everywhere in the string.
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2008-03-22 09:04:26 -03:00
|
|
|
The following applies only to regular expression objects like those created
|
|
|
|
with ``re.compile("pattern")``, not the primitives ``re.match(pattern,
|
|
|
|
string)`` or ``re.search(pattern, string)``.
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
:func:`match` has an optional second parameter that gives an index in the string
|
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
........
r69578 | georg.brandl | 2009-02-13 12:03:59 +0100 (Fr, 13 Feb 2009) | 1 line
#3694: add test for fix committed in r66693.
........
r69579 | georg.brandl | 2009-02-13 12:06:59 +0100 (Fr, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69580 | georg.brandl | 2009-02-13 12:10:04 +0100 (Fr, 13 Feb 2009) | 2 lines
Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
........
r69901 | georg.brandl | 2009-02-23 12:24:46 +0100 (Mo, 23 Feb 2009) | 2 lines
#5349: C++ pure virtuals can also have an implementation.
........
r69907 | georg.brandl | 2009-02-23 19:33:48 +0100 (Mo, 23 Feb 2009) | 1 line
Fix grammar.
........
r69994 | georg.brandl | 2009-02-26 18:36:26 +0100 (Do, 26 Feb 2009) | 1 line
Document that setting sys.py3kwarning wont do anything.
........
r70022 | georg.brandl | 2009-02-27 17:23:18 +0100 (Fr, 27 Feb 2009) | 1 line
#5361: fix typo.
........
r70023 | georg.brandl | 2009-02-27 17:39:26 +0100 (Fr, 27 Feb 2009) | 1 line
#5363: fix cmpfiles() docs. Another instance where a prose description is twice as long as the code.
........
r70025 | georg.brandl | 2009-02-27 17:52:55 +0100 (Fr, 27 Feb 2009) | 1 line
#5344: fix punctuation.
........
r70026 | georg.brandl | 2009-02-27 17:59:03 +0100 (Fr, 27 Feb 2009) | 1 line
#5365: add quick look conversion table for different time representations.
........
r70166 | georg.brandl | 2009-03-04 19:24:41 +0100 (Mi, 04 Mär 2009) | 2 lines
Remove obsolete stuff from string module docs.
........
r70273 | georg.brandl | 2009-03-09 15:25:07 +0100 (Mo, 09 Mär 2009) | 2 lines
#5458: add a note when we started to raise RuntimeErrors.
........
r70275 | georg.brandl | 2009-03-09 17:35:48 +0100 (Mo, 09 Mär 2009) | 2 lines
Add missing space.
........
r70342 | georg.brandl | 2009-03-13 20:03:58 +0100 (Fr, 13 Mär 2009) | 1 line
#5486: typos.
........
r70386 | georg.brandl | 2009-03-15 22:32:06 +0100 (So, 15 Mär 2009) | 1 line
#5496: fix docstring of lookup().
........
r70387 | georg.brandl | 2009-03-15 22:37:16 +0100 (So, 15 Mär 2009) | 1 line
#5493: clarify __nonzero__ docs.
........
r70389 | georg.brandl | 2009-03-15 22:43:38 +0100 (So, 15 Mär 2009) | 1 line
Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
........
r70390 | georg.brandl | 2009-03-15 22:44:43 +0100 (So, 15 Mär 2009) | 1 line
#5491: clarify nested() semantics.
........
r70392 | georg.brandl | 2009-03-15 22:46:00 +0100 (So, 15 Mär 2009) | 1 line
#5488: add missing struct member.
........
r70393 | georg.brandl | 2009-03-15 22:47:42 +0100 (So, 15 Mär 2009) | 1 line
#5478: fix copy-paste oversight in function signature.
........
r70395 | georg.brandl | 2009-03-15 22:51:48 +0100 (So, 15 Mär 2009) | 1 line
#5276: document IDLESTARTUP and .Idle.py.
........
r70397 | georg.brandl | 2009-03-15 22:53:56 +0100 (So, 15 Mär 2009) | 1 line
#5469: add with statement to list of name-binding constructs.
........
r70400 | georg.brandl | 2009-03-15 22:59:37 +0100 (So, 15 Mär 2009) | 3 lines
Fix markup in re docs and give a mail address in regex howto, so that
the recommendation to send suggestions to the author can be followed.
........
r70418 | georg.brandl | 2009-03-16 20:42:03 +0100 (Mo, 16 Mär 2009) | 1 line
Add token markup.
........
2009-04-05 18:48:06 -03:00
|
|
|
where the search is to start::
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> pattern = re.compile("o")
|
|
|
|
>>> pattern.match("dog") # No match as "o" is not at the start of "dog."
|
2008-03-22 09:04:26 -03:00
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
# Equivalent to the above expression as 0 is the default starting index:
|
|
|
|
>>> pattern.match("dog", 0)
|
2008-03-22 09:04:26 -03:00
|
|
|
|
2007-12-05 14:30:48 -04:00
|
|
|
# Match as "o" is the 2nd character of "dog" (index 0 is the first):
|
|
|
|
>>> pattern.match("dog", 1)
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> pattern.match("dog", 2) # No match as "o" is not the 3rd character of "dog."
|
|
|
|
|
|
|
|
|
|
|
|
Making a Phonebook
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
:func:`split` splits a string into a list delimited by the passed pattern. The
|
2007-12-05 14:30:48 -04:00
|
|
|
method is invaluable for converting textual data into data structures that can be
|
|
|
|
easily read and modified by Python as demonstrated in the following example that
|
|
|
|
creates a phonebook.
|
|
|
|
|
2007-12-06 05:45:39 -04:00
|
|
|
First, here is the input. Normally it may come from a file, here we are using
|
2008-03-22 09:04:26 -03:00
|
|
|
triple-quoted string syntax:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2007-12-06 05:45:39 -04:00
|
|
|
>>> input = """Ross McFluff: 834.345.1254 155 Elm Street
|
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line
fill in actual issue number in tests
........
r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines
Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open
file with `str' filename on Windows.
........
r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line
fix highlighting
........
r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines
welcome to 2009, Python!
........
r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines
#4801 _collections module fails to build on cygwin.
_PyObject_GC_TRACK is the macro version of PyObject_GC_Track,
and according to documentation it should not be used for extension modules.
........
r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4472: "configure --enable-shared doesn't work on OSX"
........
r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines
Forgot to add a NEWS item in my previous checkin
........
r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue4780
........
r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue 1627952
........
r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines
Fix for issue r1737832
........
r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 1149804
........
r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines
Fix for issue 4472 is incompatible with Cygwin, this patch
should fix that.
........
r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line
document PyMemberDef
........
r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines
#4811: fix markup glitches (mostly remains of the conversion),
found by Gabriel Genellina.
........
r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4075: Use OutputDebugStringW in Py_FatalError.
........
r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines
Issue #4051: Prevent conflict of UNICODE macros in cPickle.
........
r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line
fix compilation on non-Windows platforms
........
r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line
Issue #4615. Document how to use itertools for de-duping.
........
r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove useless string literal.
........
r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix indentation.
........
r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines
Set eol-style correctly for mp_distributing.py.
........
r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines
Make indentation consistent.
........
r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix role name.
........
r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines
Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources.
........
r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines
Recognize usage of the default role.
........
r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines
Fix uses of the default role.
........
r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove trailing whitespace.
........
r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines
Remove tabs from the documentation.
........
r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines
Disable the line length checker by default.
........
2009-01-03 17:55:17 -04:00
|
|
|
...
|
2008-03-22 09:04:26 -03:00
|
|
|
... Ronald Heathmore: 892.345.3428 436 Finley Avenue
|
|
|
|
... Frank Burger: 925.541.7625 662 South Dogwood Way
|
|
|
|
...
|
|
|
|
...
|
|
|
|
... Heather Albrecht: 548.326.4584 919 Park Place"""
|
2007-12-06 05:45:39 -04:00
|
|
|
|
|
|
|
The entries are separated by one or more newlines. Now we convert the string
|
2008-03-22 09:04:26 -03:00
|
|
|
into a list with each nonempty line having its own entry:
|
|
|
|
|
2008-03-22 10:07:06 -03:00
|
|
|
.. doctest::
|
2008-03-22 09:04:26 -03:00
|
|
|
:options: +NORMALIZE_WHITESPACE
|
2007-12-06 05:45:39 -04:00
|
|
|
|
|
|
|
>>> entries = re.split("\n+", input)
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> entries
|
2008-03-22 09:04:26 -03:00
|
|
|
['Ross McFluff: 834.345.1254 155 Elm Street',
|
|
|
|
'Ronald Heathmore: 892.345.3428 436 Finley Avenue',
|
|
|
|
'Frank Burger: 925.541.7625 662 South Dogwood Way',
|
|
|
|
'Heather Albrecht: 548.326.4584 919 Park Place']
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
Finally, split each entry into a list with first name, last name, telephone
|
2008-02-22 08:31:45 -04:00
|
|
|
number, and address. We use the ``maxsplit`` parameter of :func:`split`
|
2008-03-22 09:04:26 -03:00
|
|
|
because the address has spaces, our splitting pattern, in it:
|
|
|
|
|
2008-03-22 10:07:06 -03:00
|
|
|
.. doctest::
|
2008-03-22 09:04:26 -03:00
|
|
|
:options: +NORMALIZE_WHITESPACE
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2007-12-06 05:45:39 -04:00
|
|
|
>>> [re.split(":? ", entry, 3) for entry in entries]
|
2007-12-05 14:30:48 -04:00
|
|
|
[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],
|
|
|
|
['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],
|
|
|
|
['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],
|
|
|
|
['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]
|
|
|
|
|
2007-12-06 05:45:39 -04:00
|
|
|
The ``:?`` pattern matches the colon after the last name, so that it does not
|
2008-02-22 08:31:45 -04:00
|
|
|
occur in the result list. With a ``maxsplit`` of ``4``, we could separate the
|
2008-03-22 09:04:26 -03:00
|
|
|
house number from the street name:
|
|
|
|
|
2008-03-22 10:07:06 -03:00
|
|
|
.. doctest::
|
2008-03-22 09:04:26 -03:00
|
|
|
:options: +NORMALIZE_WHITESPACE
|
2007-12-05 14:30:48 -04:00
|
|
|
|
2007-12-06 05:45:39 -04:00
|
|
|
>>> [re.split(":? ", entry, 4) for entry in entries]
|
2007-12-05 14:30:48 -04:00
|
|
|
[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],
|
|
|
|
['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],
|
|
|
|
['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],
|
|
|
|
['Heather', 'Albrecht', '548.326.4584', '919', 'Park Place']]
|
|
|
|
|
|
|
|
|
|
|
|
Text Munging
|
|
|
|
^^^^^^^^^^^^
|
|
|
|
|
|
|
|
:func:`sub` replaces every occurrence of a pattern with a string or the
|
|
|
|
result of a function. This example demonstrates using :func:`sub` with
|
|
|
|
a function to "munge" text, or randomize the order of all the characters
|
|
|
|
in each word of a sentence except for the first and last characters::
|
|
|
|
|
|
|
|
>>> def repl(m):
|
|
|
|
... inner_word = list(m.group(2))
|
|
|
|
... random.shuffle(inner_word)
|
|
|
|
... return m.group(1) + "".join(inner_word) + m.group(3)
|
|
|
|
>>> text = "Professor Abdolmalek, please report your absences promptly."
|
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/release27-maint
................
r83452 | georg.brandl | 2010-08-01 23:06:46 +0200 (So, 01 Aug 2010) | 25 lines
Merged revisions 83226-83227,83229-83230,83232 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83226 | georg.brandl | 2010-07-29 16:17:12 +0200 (Do, 29 Jul 2010) | 1 line
#1090076: explain the behavior of *vars* in get() better.
........
r83227 | georg.brandl | 2010-07-29 16:23:06 +0200 (Do, 29 Jul 2010) | 1 line
Use Py_CLEAR().
........
r83229 | georg.brandl | 2010-07-29 16:32:22 +0200 (Do, 29 Jul 2010) | 1 line
#9407: document configparser.Error.
........
r83230 | georg.brandl | 2010-07-29 16:36:11 +0200 (Do, 29 Jul 2010) | 1 line
Use correct directive and name.
........
r83232 | georg.brandl | 2010-07-29 16:49:08 +0200 (Do, 29 Jul 2010) | 1 line
#9388: remove ERA_YEAR which is never defined in the source code.
........
................
r83457 | georg.brandl | 2010-08-01 23:10:57 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 83223 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83223 | georg.brandl | 2010-07-29 15:38:37 +0200 (Do, 29 Jul 2010) | 1 line
#3874: document HTMLParser.unknown_decl().
........
................
r83466 | georg.brandl | 2010-08-01 23:23:50 +0200 (So, 01 Aug 2010) | 29 lines
Merged revisions 83160-83161,83166,83168-83169,83171 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83160 | georg.brandl | 2010-07-26 10:51:42 +0200 (Mo, 26 Jul 2010) | 1 line
#9381: fix markup.
........
r83161 | georg.brandl | 2010-07-26 11:33:12 +0200 (Mo, 26 Jul 2010) | 1 line
Add Brian Quinlan.
........
r83166 | georg.brandl | 2010-07-26 17:11:49 +0200 (Mo, 26 Jul 2010) | 1 line
Fix grammar.
........
r83168 | georg.brandl | 2010-07-26 19:00:20 +0200 (Mo, 26 Jul 2010) | 1 line
Fix indentation in example.
........
r83169 | georg.brandl | 2010-07-26 19:09:32 +0200 (Mo, 26 Jul 2010) | 1 line
Add Reid.
........
r83171 | georg.brandl | 2010-07-26 23:12:13 +0200 (Mo, 26 Jul 2010) | 1 line
Clarify.
........
................
r83471 | georg.brandl | 2010-08-01 23:26:45 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 83106 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83106 | georg.brandl | 2010-07-23 18:55:26 +0200 (Fr, 23 Jul 2010) | 1 line
Fix some markup glitches.
........
................
r83475 | georg.brandl | 2010-08-01 23:28:47 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 82965 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82965 | georg.brandl | 2010-07-19 13:28:05 +0200 (Mo, 19 Jul 2010) | 1 line
Clarification. Yay importlib!
........
................
r83480 | georg.brandl | 2010-08-01 23:33:42 +0200 (So, 01 Aug 2010) | 17 lines
Merged revisions 82871,82960-82961 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82871 | georg.brandl | 2010-07-14 10:00:22 +0200 (Mi, 14 Jul 2010) | 1 line
#9258: fix copy-paste errors.
........
r82960 | georg.brandl | 2010-07-19 08:52:35 +0200 (Mo, 19 Jul 2010) | 1 line
Clarify.
........
r82961 | georg.brandl | 2010-07-19 08:57:52 +0200 (Mo, 19 Jul 2010) | 1 line
Clarify :option: description.
........
................
r83486 | georg.brandl | 2010-08-01 23:44:38 +0200 (So, 01 Aug 2010) | 13 lines
Merged revisions 82832,82834 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82832 | georg.brandl | 2010-07-12 11:00:29 +0200 (Mo, 12 Jul 2010) | 1 line
Take care of duplicate target name warnings.
........
r82834 | georg.brandl | 2010-07-12 11:06:13 +0200 (Mo, 12 Jul 2010) | 1 line
Use raw string literals for regexes containing backlash.
........
................
2010-08-01 18:48:47 -03:00
|
|
|
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
|
2007-12-05 14:30:48 -04:00
|
|
|
'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'
|
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/release27-maint
................
r83452 | georg.brandl | 2010-08-01 23:06:46 +0200 (So, 01 Aug 2010) | 25 lines
Merged revisions 83226-83227,83229-83230,83232 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83226 | georg.brandl | 2010-07-29 16:17:12 +0200 (Do, 29 Jul 2010) | 1 line
#1090076: explain the behavior of *vars* in get() better.
........
r83227 | georg.brandl | 2010-07-29 16:23:06 +0200 (Do, 29 Jul 2010) | 1 line
Use Py_CLEAR().
........
r83229 | georg.brandl | 2010-07-29 16:32:22 +0200 (Do, 29 Jul 2010) | 1 line
#9407: document configparser.Error.
........
r83230 | georg.brandl | 2010-07-29 16:36:11 +0200 (Do, 29 Jul 2010) | 1 line
Use correct directive and name.
........
r83232 | georg.brandl | 2010-07-29 16:49:08 +0200 (Do, 29 Jul 2010) | 1 line
#9388: remove ERA_YEAR which is never defined in the source code.
........
................
r83457 | georg.brandl | 2010-08-01 23:10:57 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 83223 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83223 | georg.brandl | 2010-07-29 15:38:37 +0200 (Do, 29 Jul 2010) | 1 line
#3874: document HTMLParser.unknown_decl().
........
................
r83466 | georg.brandl | 2010-08-01 23:23:50 +0200 (So, 01 Aug 2010) | 29 lines
Merged revisions 83160-83161,83166,83168-83169,83171 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83160 | georg.brandl | 2010-07-26 10:51:42 +0200 (Mo, 26 Jul 2010) | 1 line
#9381: fix markup.
........
r83161 | georg.brandl | 2010-07-26 11:33:12 +0200 (Mo, 26 Jul 2010) | 1 line
Add Brian Quinlan.
........
r83166 | georg.brandl | 2010-07-26 17:11:49 +0200 (Mo, 26 Jul 2010) | 1 line
Fix grammar.
........
r83168 | georg.brandl | 2010-07-26 19:00:20 +0200 (Mo, 26 Jul 2010) | 1 line
Fix indentation in example.
........
r83169 | georg.brandl | 2010-07-26 19:09:32 +0200 (Mo, 26 Jul 2010) | 1 line
Add Reid.
........
r83171 | georg.brandl | 2010-07-26 23:12:13 +0200 (Mo, 26 Jul 2010) | 1 line
Clarify.
........
................
r83471 | georg.brandl | 2010-08-01 23:26:45 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 83106 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83106 | georg.brandl | 2010-07-23 18:55:26 +0200 (Fr, 23 Jul 2010) | 1 line
Fix some markup glitches.
........
................
r83475 | georg.brandl | 2010-08-01 23:28:47 +0200 (So, 01 Aug 2010) | 9 lines
Merged revisions 82965 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82965 | georg.brandl | 2010-07-19 13:28:05 +0200 (Mo, 19 Jul 2010) | 1 line
Clarification. Yay importlib!
........
................
r83480 | georg.brandl | 2010-08-01 23:33:42 +0200 (So, 01 Aug 2010) | 17 lines
Merged revisions 82871,82960-82961 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82871 | georg.brandl | 2010-07-14 10:00:22 +0200 (Mi, 14 Jul 2010) | 1 line
#9258: fix copy-paste errors.
........
r82960 | georg.brandl | 2010-07-19 08:52:35 +0200 (Mo, 19 Jul 2010) | 1 line
Clarify.
........
r82961 | georg.brandl | 2010-07-19 08:57:52 +0200 (Mo, 19 Jul 2010) | 1 line
Clarify :option: description.
........
................
r83486 | georg.brandl | 2010-08-01 23:44:38 +0200 (So, 01 Aug 2010) | 13 lines
Merged revisions 82832,82834 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82832 | georg.brandl | 2010-07-12 11:00:29 +0200 (Mo, 12 Jul 2010) | 1 line
Take care of duplicate target name warnings.
........
r82834 | georg.brandl | 2010-07-12 11:06:13 +0200 (Mo, 12 Jul 2010) | 1 line
Use raw string literals for regexes containing backlash.
........
................
2010-08-01 18:48:47 -03:00
|
|
|
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
|
2007-12-05 14:30:48 -04:00
|
|
|
'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.'
|
|
|
|
|
|
|
|
|
|
|
|
Finding all Adverbs
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2008-02-22 08:31:45 -04:00
|
|
|
:func:`findall` matches *all* occurrences of a pattern, not just the first
|
2007-12-05 14:30:48 -04:00
|
|
|
one as :func:`search` does. For example, if one was a writer and wanted to
|
|
|
|
find all of the adverbs in some text, he or she might use :func:`findall` in
|
2008-03-22 09:04:26 -03:00
|
|
|
the following manner:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> text = "He was carefully disguised but captured quickly by police."
|
|
|
|
>>> re.findall(r"\w+ly", text)
|
|
|
|
['carefully', 'quickly']
|
|
|
|
|
|
|
|
|
|
|
|
Finding all Adverbs and their Positions
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
If one wants more information about all matches of a pattern than the matched
|
|
|
|
text, :func:`finditer` is useful as it provides instances of
|
|
|
|
:class:`MatchObject` instead of strings. Continuing with the previous example,
|
|
|
|
if one was a writer who wanted to find all of the adverbs *and their positions*
|
2008-03-22 09:04:26 -03:00
|
|
|
in some text, he or she would use :func:`finditer` in the following manner:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> text = "He was carefully disguised but captured quickly by police."
|
|
|
|
>>> for m in re.finditer(r"\w+ly", text):
|
2008-03-22 09:04:26 -03:00
|
|
|
... print '%02d-%02d: %s' % (m.start(), m.end(), m.group(0))
|
2007-12-05 14:30:48 -04:00
|
|
|
07-16: carefully
|
|
|
|
40-47: quickly
|
|
|
|
|
|
|
|
|
|
|
|
Raw String Notation
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Raw string notation (``r"text"``) keeps regular expressions sane. Without it,
|
|
|
|
every backslash (``'\'``) in a regular expression would have to be prefixed with
|
|
|
|
another one to escape it. For example, the two following lines of code are
|
2008-03-22 09:04:26 -03:00
|
|
|
functionally identical:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> re.match(r"\W(.)\1\W", " ff ")
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> re.match("\\W(.)\\1\\W", " ff ")
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
When one wants to match a literal backslash, it must be escaped in the regular
|
|
|
|
expression. With raw string notation, this means ``r"\\"``. Without raw string
|
|
|
|
notation, one must use ``"\\\\"``, making the following lines of code
|
2008-03-22 09:04:26 -03:00
|
|
|
functionally identical:
|
2007-12-05 14:30:48 -04:00
|
|
|
|
|
|
|
>>> re.match(r"\\", r"\\")
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|
2007-12-05 14:30:48 -04:00
|
|
|
>>> re.match("\\\\", r"\\")
|
2008-03-22 09:04:26 -03:00
|
|
|
<_sre.SRE_Match object at ...>
|