@@ -856,18 +856,17 @@ Functions
856
856
.. function :: search(pattern, string, flags=0)
857
857
858
858
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.
863
862
864
863
865
864
.. function :: match(pattern, string, flags=0)
866
865
867
866
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.
871
870
872
871
Note that even in :const: `MULTILINE ` mode, :func: `re.match ` will only match
873
872
at the beginning of the string and not at the beginning of each line.
@@ -879,9 +878,8 @@ Functions
879
878
.. function :: fullmatch(pattern, string, flags=0)
880
879
881
880
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.
885
883
886
884
.. versionadded :: 3.4
887
885
@@ -959,7 +957,7 @@ Functions
959
957
960
958
.. function :: finditer(pattern, string, flags=0)
961
959
962
- Return an :term: `iterator ` yielding :ref: ` match objects < match-objects >` over
960
+ Return an :term: `iterator ` yielding :class: ` ~re.Match ` objects over
963
961
all non-overlapping matches for the RE *pattern * in *string *. The *string *
964
962
is scanned left-to-right, and matches are returned in the order found. Empty
965
963
matches are included in the result.
@@ -987,8 +985,8 @@ Functions
987
985
'static PyObject*\npy_myfunc(void)\n{'
988
986
989
987
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::
992
990
993
991
>>> def dashrepl(matchobj):
994
992
... if matchobj.group(0) == '-': return ' '
@@ -999,7 +997,7 @@ Functions
999
997
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
1000
998
'Baked Beans & Spam'
1001
999
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 `.
1003
1001
1004
1002
The optional argument *count * is the maximum number of pattern occurrences to be
1005
1003
replaced; *count * must be a non-negative integer. If omitted or zero, all
@@ -1131,16 +1129,20 @@ Exceptions
1131
1129
Regular Expression Objects
1132
1130
--------------------------
1133
1131
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 `.
1136
1139
1137
1140
.. method :: Pattern.search(string[, pos[, endpos]])
1138
1141
1139
1142
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.
1144
1146
1145
1147
The optional second parameter *pos * gives an index in the string where the
1146
1148
search is to start; it defaults to ``0 ``. This is not completely equivalent to
@@ -1164,9 +1166,9 @@ attributes:
1164
1166
.. method :: Pattern.match(string[, pos[, endpos]])
1165
1167
1166
1168
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.
1170
1172
1171
1173
The optional *pos * and *endpos * parameters have the same meaning as for the
1172
1174
:meth: `~Pattern.search ` method. ::
@@ -1183,8 +1185,8 @@ attributes:
1183
1185
.. method :: Pattern.fullmatch(string[, pos[, endpos]])
1184
1186
1185
1187
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.
1188
1190
1189
1191
The optional *pos * and *endpos * parameters have the same meaning as for the
1190
1192
:meth: `~Pattern.search ` method. ::
@@ -1270,8 +1272,13 @@ when there is no match, you can test whether there was a match with a simple
1270
1272
if match:
1271
1273
process(match)
1272
1274
1273
- Match objects support the following methods and attributes:
1275
+ .. class :: Match
1276
+
1277
+ Match object returned by successful ``match ``\ es and ``search ``\ es.
1274
1278
1279
+ .. versionchanged :: 3.9
1280
+ :py:class: `re.Match ` supports ``[] `` to indicate a Unicode (str) or bytes match.
1281
+ See :ref: `types-genericalias `.
1275
1282
1276
1283
.. method :: Match.expand(template)
1277
1284
@@ -1715,10 +1722,10 @@ Finding all Adverbs and their Positions
1715
1722
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1716
1723
1717
1724
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::
1722
1729
1723
1730
>>> text = "He was carefully disguised but captured quickly by police."
1724
1731
>>> for m in re.finditer(r"\w+ly\b", text):
0 commit comments