@@ -154,6 +154,30 @@ The special characters are:
154154 characters as possible will be matched. Using the RE ``<.*?> `` will match
155155 only ``'<a>' ``.
156156
157+ .. index ::
158+ single: *+; in regular expressions
159+ single: ++; in regular expressions
160+ single: ?+; in regular expressions
161+
162+ ``*+ ``, ``++ ``, ``?+ ``
163+ Like the ``'*' ``, ``'+' ``, and ``'?' `` qualifiers, those where ``'+' `` is
164+ appended also match as many times as possible.
165+ However, unlike the true greedy qualifiers, these do not allow
166+ back-tracking when the expression following it fails to match.
167+ These are known as :dfn: `possessive ` qualifiers.
168+ For example, ``a*a `` will match ``'aaaa' `` because the ``a* `` will match
169+ all 4 ``'a'``s, but, when the final ``'a' `` is encountered, the
170+ expression is backtracked so that in the end the ``a* `` ends up matching
171+ 3 ``'a'``s total, and the fourth ``'a' `` is matched by the final ``'a' ``.
172+ However, when ``a*+a `` is used to match ``'aaaa' ``, the ``a*+ `` will
173+ match all 4 ``'a' ``, but when the final ``'a' `` fails to find any more
174+ characters to match, the expression cannot be backtracked and will thus
175+ fail to match.
176+ ``x*+ ``, ``x++ `` and ``x?+ `` are equivalent to ``(?>x*) ``, ``(?>x+) ``
177+ and ``(?>x?) `` correspondigly.
178+
179+ .. versionadded :: 3.11
180+
157181.. index ::
158182 single: {} (curly brackets); in regular expressions
159183
@@ -178,6 +202,21 @@ The special characters are:
178202 6-character string ``'aaaaaa' ``, ``a{3,5} `` will match 5 ``'a' `` characters,
179203 while ``a{3,5}? `` will only match 3 characters.
180204
205+ ``{m,n}+ ``
206+ Causes the resulting RE to match from *m * to *n * repetitions of the
207+ preceding RE, attempting to match as many repetitions as possible
208+ *without * establishing any backtracking points.
209+ This is the possessive version of the qualifier above.
210+ For example, on the 6-character string ``'aaaaaa' ``, ``a{3,5}+aa ``
211+ attempt to match 5 ``'a' `` characters, then, requiring 2 more ``'a'``s,
212+ will need more characters than available and thus fail, while
213+ ``a{3,5}aa `` will match with ``a{3,5} `` capturing 5, then 4 ``'a'``s
214+ by backtracking and then the final 2 ``'a'``s are matched by the final
215+ ``aa `` in the pattern.
216+ ``x{m,n}+ `` is equivalent to ``(?>x{m,n}) ``.
217+
218+ .. versionadded :: 3.11
219+
181220.. index :: single: \ (backslash); in regular expressions
182221
183222``\ ``
@@ -336,6 +375,21 @@ The special characters are:
336375 .. versionchanged :: 3.7
337376 The letters ``'a' ``, ``'L' `` and ``'u' `` also can be used in a group.
338377
378+ ``(?>...) ``
379+ Attempts to match ``... `` as if it was a separate regular expression, and
380+ if successful, continues to match the rest of the pattern following it.
381+ If the subsequent pattern fails to match, the stack can only be unwound
382+ to a point *before * the ``(?>...) `` because once exited, the expression,
383+ known as an :dfn: `atomic group `, has thrown away all stack points within
384+ itself.
385+ Thus, ``(?>.*). `` would never match anything because first the ``.* ``
386+ would match all characters possible, then, having nothing left to match,
387+ the final ``. `` would fail to match.
388+ Since there are no stack points saved in the Atomic Group, and there is
389+ no stack point before it, the entire expression would thus fail to match.
390+
391+ .. versionadded :: 3.11
392+
339393.. index :: single: (?P<; in regular expressions
340394
341395``(?P<name>...) ``
0 commit comments