Skip to content

Commit 6895ddf

Browse files
flying-sheepJelleZijlstraAA-TurnerAlexWaygood
authored
gh-102211: Document re.{Pattern,Match}’s existence (#102212)
Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 5347018 commit 6895ddf

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

Doc/library/re.rst

+37-30
Original file line numberDiff line numberDiff line change
@@ -856,18 +856,17 @@ Functions
856856
.. function:: search(pattern, string, flags=0)
857857

858858
Scan through *string* looking for the first location where the regular expression
859-
*pattern* produces a match, and return a corresponding :ref:`match object
860-
<match-objects>`. Return ``None`` if no position in the string matches the
861-
pattern; note that this is different from finding a zero-length match at some
862-
point in the string.
859+
*pattern* produces a match, and return a corresponding :class:`~re.Match`. Return
860+
``None`` if no position in the string matches the pattern; note that this is
861+
different from finding a zero-length match at some point in the string.
863862

864863

865864
.. function:: match(pattern, string, flags=0)
866865

867866
If zero or more characters at the beginning of *string* match the regular
868-
expression *pattern*, return a corresponding :ref:`match object
869-
<match-objects>`. Return ``None`` if the string does not match the pattern;
870-
note that this is different from a zero-length match.
867+
expression *pattern*, return a corresponding :class:`~re.Match`. Return
868+
``None`` if the string does not match the pattern; note that this is
869+
different from a zero-length match.
871870

872871
Note that even in :const:`MULTILINE` mode, :func:`re.match` will only match
873872
at the beginning of the string and not at the beginning of each line.
@@ -879,9 +878,8 @@ Functions
879878
.. function:: fullmatch(pattern, string, flags=0)
880879

881880
If the whole *string* matches the regular expression *pattern*, return a
882-
corresponding :ref:`match object <match-objects>`. Return ``None`` if the
883-
string does not match the pattern; note that this is different from a
884-
zero-length match.
881+
corresponding :class:`~re.Match`. Return ``None`` if the string does not match
882+
the pattern; note that this is different from a zero-length match.
885883

886884
.. versionadded:: 3.4
887885

@@ -959,7 +957,7 @@ Functions
959957

960958
.. function:: finditer(pattern, string, flags=0)
961959

962-
Return an :term:`iterator` yielding :ref:`match objects <match-objects>` over
960+
Return an :term:`iterator` yielding :class:`~re.Match` objects over
963961
all non-overlapping matches for the RE *pattern* in *string*. The *string*
964962
is scanned left-to-right, and matches are returned in the order found. Empty
965963
matches are included in the result.
@@ -987,8 +985,8 @@ Functions
987985
'static PyObject*\npy_myfunc(void)\n{'
988986

989987
If *repl* is a function, it is called for every non-overlapping occurrence of
990-
*pattern*. The function takes a single :ref:`match object <match-objects>`
991-
argument, and returns the replacement string. For example::
988+
*pattern*. The function takes a single :class:`~re.Match` argument, and returns
989+
the replacement string. For example::
992990

993991
>>> def dashrepl(matchobj):
994992
... if matchobj.group(0) == '-': return ' '
@@ -999,7 +997,7 @@ Functions
999997
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
1000998
'Baked Beans & Spam'
1001999

1002-
The pattern may be a string or a :ref:`pattern object <re-objects>`.
1000+
The pattern may be a string or a :class:`~re.Pattern`.
10031001

10041002
The optional argument *count* is the maximum number of pattern occurrences to be
10051003
replaced; *count* must be a non-negative integer. If omitted or zero, all
@@ -1131,16 +1129,20 @@ Exceptions
11311129
Regular Expression Objects
11321130
--------------------------
11331131

1134-
Compiled regular expression objects support the following methods and
1135-
attributes:
1132+
.. class:: Pattern
1133+
1134+
Compiled regular expression object returned by :func:`re.compile`.
1135+
1136+
.. versionchanged:: 3.9
1137+
:py:class:`re.Pattern` supports ``[]`` to indicate a Unicode (str) or bytes pattern.
1138+
See :ref:`types-genericalias`.
11361139

11371140
.. method:: Pattern.search(string[, pos[, endpos]])
11381141

11391142
Scan through *string* looking for the first location where this regular
1140-
expression produces a match, and return a corresponding :ref:`match object
1141-
<match-objects>`. Return ``None`` if no position in the string matches the
1142-
pattern; note that this is different from finding a zero-length match at some
1143-
point in the string.
1143+
expression produces a match, and return a corresponding :class:`~re.Match`.
1144+
Return ``None`` if no position in the string matches the pattern; note that
1145+
this is different from finding a zero-length match at some point in the string.
11441146

11451147
The optional second parameter *pos* gives an index in the string where the
11461148
search is to start; it defaults to ``0``. This is not completely equivalent to
@@ -1164,9 +1166,9 @@ attributes:
11641166
.. method:: Pattern.match(string[, pos[, endpos]])
11651167

11661168
If zero or more characters at the *beginning* of *string* match this regular
1167-
expression, return a corresponding :ref:`match object <match-objects>`.
1168-
Return ``None`` if the string does not match the pattern; note that this is
1169-
different from a zero-length match.
1169+
expression, return a corresponding :class:`~re.Match`. Return ``None`` if the
1170+
string does not match the pattern; note that this is different from a
1171+
zero-length match.
11701172

11711173
The optional *pos* and *endpos* parameters have the same meaning as for the
11721174
:meth:`~Pattern.search` method. ::
@@ -1183,8 +1185,8 @@ attributes:
11831185
.. method:: Pattern.fullmatch(string[, pos[, endpos]])
11841186

11851187
If the whole *string* matches this regular expression, return a corresponding
1186-
:ref:`match object <match-objects>`. Return ``None`` if the string does not
1187-
match the pattern; note that this is different from a zero-length match.
1188+
:class:`~re.Match`. Return ``None`` if the string does not match the pattern;
1189+
note that this is different from a zero-length match.
11881190

11891191
The optional *pos* and *endpos* parameters have the same meaning as for the
11901192
:meth:`~Pattern.search` method. ::
@@ -1270,8 +1272,13 @@ when there is no match, you can test whether there was a match with a simple
12701272
if match:
12711273
process(match)
12721274

1273-
Match objects support the following methods and attributes:
1275+
.. class:: Match
1276+
1277+
Match object returned by successful ``match``\ es and ``search``\ es.
12741278

1279+
.. versionchanged:: 3.9
1280+
:py:class:`re.Match` supports ``[]`` to indicate a Unicode (str) or bytes match.
1281+
See :ref:`types-genericalias`.
12751282

12761283
.. method:: Match.expand(template)
12771284

@@ -1715,10 +1722,10 @@ Finding all Adverbs and their Positions
17151722
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17161723

17171724
If one wants more information about all matches of a pattern than the matched
1718-
text, :func:`finditer` is useful as it provides :ref:`match objects
1719-
<match-objects>` instead of strings. Continuing with the previous example, if
1720-
a writer wanted to find all of the adverbs *and their positions* in
1721-
some text, they would use :func:`finditer` in the following manner::
1725+
text, :func:`finditer` is useful as it provides :class:`~re.Match` objects
1726+
instead of strings. Continuing with the previous example, if a writer wanted
1727+
to find all of the adverbs *and their positions* in some text, they would use
1728+
:func:`finditer` in the following manner::
17221729

17231730
>>> text = "He was carefully disguised but captured quickly by police."
17241731
>>> for m in re.finditer(r"\w+ly\b", text):

0 commit comments

Comments
 (0)