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

Capturing group doesn't exist message could suggest the solution when the cause is replacement confusion #501

Open
i30817 opened this issue Jun 13, 2023 · 0 comments

Comments

@i30817
Copy link

i30817 commented Jun 13, 2023

I was being kind of a idiot and was doing:

def replaceRoman(source, romana, number):
    source = regex.sub(rf"([\s']|^){romana}([\s,]|$)", rf"\1{number}\2", source)
    return source

This fails in both here and re because the 'number' being added changes the first capturing group

the solution is

def replaceRoman(source, romana, number):
    source = regex.sub(rf"([\s']|^){romana}([\s,]|$)", rf"\g<1>{number}\g<2>", source)
    return source

Maybe the exception message can print out the final text and the part that is supposed to be the 'failing group', then suggest the \g<#> construct? I'll be honest, i didn't immediately get 'group 13' was the error because i was automatic coding.

@i30817 i30817 changed the title Not sure if this could be done better Capturing group doesn't exist message could suggest the solution when the cause is replacement confusion Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant