search() on a compiled regular expression ignores the "pos" argument
With MicroPython v1.16 (on 2021-06-18; Raspberry Pi Pico with RP2040), I observe the following unexpected output from .search() with a pos argument on a compiled regular expression:
>>> import re
>>> RE = re.compile("a")
>>> print(RE.search("ab", 1) is None)
False # Should be True
Same result on MicroPython v1.12-1 on 2020-02-09; linux version. Contrast with Python 3.8.10:
>>> import re
>>> RE = re.compile("a")
>>> print(RE.search("ab", 1) is None)
True
It seems that the pos parameter is ignored in MicroPython?
>>> print(RE.search("aa", 1).span())
(0, 1) # Should be (1, 2)
re: Add support for start- and endpos.
This adds support to match the CPython re module which supports searching and matching a substring using a start- and end-pos specified in the corresponding Pattern method.
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 of the string, which is advantageous for performance and also makes inroads for something like the finditer method.
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.