-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
re.sub does only first 16 replacements if re.S is used #66949
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
Comments
Easily reproduced: re.sub('x', 'a', "x"*20, re.S) returns 'aaaaaaaaaaaaaaaaxxxx' |
This is not a bug; the signature of re.sub() is sub(pattern, repl, string, count=0, flags=0) and the fourth argument thus the 'count' delimiting the number of substitutions that you accidentally specify as >>> import re
>>> re.S
16 I recommend that you use a keyword arg to fix your code: >>> re.sub('x', 'a', "x"*20, flags=re.S)
'aaaaaaaaaaaaaaaaaaaa' |
The fourth parameter is not "flags", but "count", the max. number of substitutions. "flags" is the fifth parameter, or give it as a keyword argument. |
This bug report is common. An enhancement would be to make the count parameter a keyword only parameter. Would it break a lot of code? |
See bpo-11957. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: