extmod/modre: Add support for start- and endpos.

Pattern objects have two additional parameters for the ::search and ::match
methods to define the starting and ending position of the subject within
the string to be searched.

This allows for searching a sub-string without creating a slice.  However,
one caveat of using the start-pos rather than a slice is that the start
anchor (`^`) remains anchored to the beginning of the text.

Signed-off-by: Jared Hancock <jared@greezybacon.me>
This commit is contained in:
Jared Hancock
2024-03-25 20:58:51 -05:00
committed by Damien George
parent 485dac783b
commit 14ccdeb4d7
3 changed files with 114 additions and 3 deletions

View File

@@ -154,8 +154,8 @@ Regex objects
Compiled regular expression. Instances of this class are created using
`re.compile()`.
.. method:: regex.match(string)
regex.search(string)
.. method:: regex.match(string, [pos, [endpos]])
regex.search(string, [pos, [endpos]])
regex.sub(replace, string, count=0, flags=0, /)
Similar to the module-level functions :meth:`match`, :meth:`search`
@@ -163,6 +163,16 @@ Compiled regular expression. Instances of this class are created using
Using methods is (much) more efficient if the same regex is applied to
multiple strings.
The optional second parameter *pos* gives an index in the string where the
search is to start; it defaults to ``0``. This is not completely equivalent
to slicing the string; the ``'^'`` pattern character matches at the real
beginning of the string and at positions just after a newline, but not
necessarily at the index where the search is to start.
The optional parameter *endpos* limits how far the string will be searched;
it will be as if the string is *endpos* characters long, so only the
characters from *pos* to ``endpos - 1`` will be searched for a match.
.. method:: regex.split(string, max_split=-1, /)
Split a *string* using regex. If *max_split* is given, it specifies