Skip to content

Commit 4727bd1

Browse files
committed
docs/ure: Document sub(), groups(), span(), start() and end().
1 parent 79d5e3a commit 4727bd1

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

‎docs/library/ure.rst‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ Functions
6464
string for first position which matches regex (which still may be
6565
0 if regex is anchored).
6666

67+
.. function:: sub(regex_str, replace, string, count=0, flags=0)
68+
69+
Compile *regex_str* and search for it in *string*, replacing all matches
70+
with *replace*, and returning the new string.
71+
72+
*replace* can be a string or a function. If it is a string then escape
73+
sequences of the form ``\<number>`` and ``\g<number>`` can be used to
74+
expand to the corresponding group (or an empty string for unmatched groups).
75+
If *replace* is a function then it must take a single argument (the match)
76+
and should return a replacement string.
77+
78+
If *count* is specified and non-zero then substitution will stop after
79+
this many substitutions are made. The *flags* argument is ignored.
80+
81+
Note: availability of this function depends on `MicroPython port`.
82+
6783
.. data:: DEBUG
6884

6985
Flag value, display debug information about compiled expression.
@@ -80,8 +96,10 @@ Compiled regular expression. Instances of this class are created using
8096

8197
.. method:: regex.match(string)
8298
regex.search(string)
99+
regex.sub(replace, string, count=0, flags=0)
83100

84-
Similar to the module-level functions :meth:`match` and :meth:`search`.
101+
Similar to the module-level functions :meth:`match`, :meth:`search`
102+
and :meth:`sub`.
85103
Using methods is (much) more efficient if the same regex is applied to
86104
multiple strings.
87105

@@ -94,9 +112,31 @@ Compiled regular expression. Instances of this class are created using
94112
Match objects
95113
-------------
96114

97-
Match objects as returned by `match()` and `search()` methods.
115+
Match objects as returned by `match()` and `search()` methods, and passed
116+
to the replacement function in `sub()`.
98117

99118
.. method:: match.group([index])
100119

101120
Return matching (sub)string. *index* is 0 for entire match,
102121
1 and above for each capturing group. Only numeric groups are supported.
122+
123+
.. method:: match.groups()
124+
125+
Return a tuple containing all the substrings of the groups of the match.
126+
127+
Note: availability of this method depends on `MicroPython port`.
128+
129+
.. method:: match.start([index])
130+
match.end([index])
131+
132+
Return the index in the original string of the start or end of the
133+
substring group that was matched. *index* defaults to the entire
134+
group, otherwise it will select a group.
135+
136+
Note: availability of these methods depends on `MicroPython port`.
137+
138+
.. method:: match.span([index])
139+
140+
Returns the 2-tuple ``(match.start(index), match.end(index))``.
141+
142+
Note: availability of this method depends on `MicroPython port`.

0 commit comments

Comments
 (0)