[docs] [issue19055] Clarify docs for re module: why * does not match as many repetitions as possible.
report at bugs.python.org
Mon Aug 11 11:27:32 CEST 2014
Akira Li added the comment:
tl;dr: added patch that clarifies Python re behavior. Please, review.
The documented behavior is not clear: why (a|ab)* is not equivalent to
(a|ab)(a|ab) for aba if the docs say "as many repetitions as are
And it is not obvious (it is not the only possible behavior) e.g.,
POSIX  expects the longest match, PCRE  group may be atomic
(/possesive quantifiers), or there is ungreedy repetition:
$ echo abac | grep -o '\(a\|ab\)* # Basic Regular Expression
$ echo abac | grep -Eo '(a|ab)*' # Extended Regular Expression
$ echo abac | grep -Po '(a|ab)*' # PCRE (like Python regexes)
$ echo abac | grep -Po '(a|ab)(a|ab)' # PCRE (two repeats)
$ echo abac | grep -Po '(a|ab)*c' # PCRE (re-eval to match the rest)
$ echo abAc | grep -iPo '(a|ab)*+c' # PCRE (possessive quantifiers)
$ echo abac | grep -Po '(a|ab)*?' # PCRE (ungreedy, zero repeats)
# matches nothing
where BREs, EREs are defined in  that says:
If the pattern permits a variable number of matching characters and
thus there is more than one such sequence starting at that point,
*the longest such sequence is matched*.
Consistent with the whole match being the longest of the leftmost
matches, each subpattern, from left to right, *shall match the
longest possible string.*
Python regexes work like PCRE  that says:
The matching process tries each alternative in turn, from left to
right, and *the first one that succeeds is used*. If the
alternatives are within a subpattern (defined below), "succeeds"
means matching the rest of the main pattern as well as the
alternative in the subpattern.
It explains why (a|ab)* matches only a in aba ("the first one that
succeeds") and why at the same time (a|ab)*c matches abac: (a|ab) may
match ab in the latter case because the main pattern would fail
otherwise i.e., the advanced definition of "succeeds" is used:
""succeeds" means matching the rest of the main pattern ...".
Python docs  are similar but do not contain the "advanced"
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.
It explains why (a|ab) matches a in aba.
'*' behavior :
By default, the quantifiers are "greedy", that is, they match as
much as possible (up to the maximum number of permitted times),
without causing the rest of the pattern to fail.
and again Python docs  are similar:
``'*'`` Causes the resulting RE to match 0 or more repetitions of
the preceding RE, as many repetitions as are possible.
It implies that (a|ab)* should be equivalent to (a|ab)(a|ab) to match
aba -- TWO REPETITIONS ARE MORE THAN ONE: "as many repetitions as are
possible". But it matches only 'a' i.e., it works as (a|ab) -- only
one repetition instead of two. In reality, (a|ab)* along works like a*.
I've uploaded a documentation patch that makes Python documentation
for '|' more similar to PCRE and clarifies '*' definition as R. David
Murray suggested in msg198126. Please, review.
components: -Regular Expressions
title: Regular expressions: * does not match as many repetitions as possible. -> Clarify docs for re module: why * does not match as many repetitions as possible.
versions: +Python 3.5 -Python 2.7, Python 3.3
Added file: http://bugs.python.org/file36340/re-docs-repetitions.patch
Python tracker <report at bugs.python.org>
More information about the docs