← index #7502Issue #11724
Related · medium · value 0.642
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 · ISSUE

ure returns different results than using re (Regular expression)

openby maxi07opened 2023-06-07updated 2023-06-11
bugextmod

Hello,
this is my function to validate for HEX colors.

try:
    import ure as re
except ModuleNotFoundError:
    import re
def is_valid_color(color: str) -> bool:
    """Validate the color format using a regular expression"""
    try:
        color_pattern = r'^#[0-9a-fA-F]{6}$'
        return re.match(color_pattern, color) is not None
    except Exception as ex:
        print("Failed validating color:", ex)
        return False

print(is_valid_color("#00ff33"))

When this code is executed using micropython 1.20.0, the function returns False. When run with Python 3.11, the function returns True. I was expecting a True result, as the regular expression does match with the search string. This can be verified using Regex101, which does find a match.

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