Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex start of string replace seems to be repeatedly applied #8402

Closed
Neradoc opened this issue Mar 9, 2022 · 4 comments
Closed

Regex start of string replace seems to be repeatedly applied #8402

Neradoc opened this issue Mar 9, 2022 · 4 comments
Labels

Comments

@Neradoc
Copy link

Neradoc commented Mar 9, 2022

In MicroPython on ESP32 (v1.18 release and v1.18-202-gade2720e5).
Start-of-string ^a regex in sub acts like it's applied repeatedly in some way.

MicroPython v1.18 on 2022-03-09; ESP32 module with ESP32
Type "help()" for more information.
>>> import re
>>> re.sub('^ab', "0", "abababcabab")
'000cabab'

In C Python we get the expected result:

>>> import re
>>> re.sub('^ab', "0", "abababcabab")
'0ababcabab'
@dpgeorge
Copy link
Member

dpgeorge commented Mar 10, 2022

Thanks for the report, I can confirm the issue. The problem is that the sub() function keeps resetting (after a substitute) the start of the string that its scanning, so the ^ keeps matching.

A slightly more complicated example of the issue, showing that it's not simple to solve:

>>> re.sub('(^ab|cab)', '0', 'abababcabab')
'00000'

@dpgeorge
Copy link
Member

Also similar issue for split():

print(re.compile('^ab').split('abababcabab'))
['', '', '', 'cabab']

dpgeorge added a commit to dpgeorge/micropython that referenced this issue Mar 10, 2022
Fixes issue micropython#8402.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
Copy link
Member

I found a relatively simple fix; see #8403.

dpgeorge added a commit to dpgeorge/micropython that referenced this issue Mar 16, 2022
Fixes issue micropython#8402.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
Copy link
Member

Fixed by 63f0e70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants