← index #7502PR #14179
Likely Duplicate · high · value 0.482
QUERY · ISSUE

search() on a compiled regular expression ignores the "pos" argument

openby cpitclaudelopened 2021-07-07updated 2023-02-26
extmod

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)
CANDIDATE · PULL REQUEST

re: Add support for start- and endpos.

mergedby greezybaconopened 2024-03-26updated 2025-08-11
extmod

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied