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

re.sub() doesn't substitute for all words with the replacement in the string when re.IGNORECASE is used and counter not specified #99918

Closed
abhii2003 opened this issue Dec 1, 2022 · 5 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@abhii2003
Copy link

abhii2003 commented Dec 1, 2022

# Bug report

re.sub() doesn't substitute for all words with the replacement in the string when re.IGNORECASE is used.

# Your environment
This bug was experienced on IDLE and VS code

#Problem
Whenever re.sub() is used with the flag re.IGNORECASE or re.I and, the counter is not specified all of the occurrences of the word were not replaced instead only the first 2 occurrences were replaced.

For example if the replacement is a string "cat" which should replace the word dog,
The code :
import re
print(re.sub("dog","cat","dog and cat and dog and dog and dog",re.I))

or
import re
print(re.sub("dog","cat","dog and cat and dog and dog and dog",re.IGNORECASE))

the expected output was :
cat and cat and cat and cat and cat

the actual output is:
cat and cat and cat and dog and dog

below is the image of the editor depicting the problem

image

@abhii2003 abhii2003 added the type-bug An unexpected behavior, bug, or error label Dec 1, 2022
@abhii2003
Copy link
Author

Can someone please help and guide me to contribute to solving the issue?

@hauntsaninja
Copy link
Contributor

Duplicate of #85830

I recommend using keyword args: print(re.sub("dog","cat","dog and cat and dog and dog and dog", flags=re.I))

I'm leaving open in case anyone is interested in deprecating passing count by position — this is a little bit of a footgun

@hauntsaninja
Copy link
Contributor

Okay, looks like this is a super common point of confusion, so we should do something. Time to go through the following issues and figure out what Serhiy's opinion on this: #89621, #86639, #85930, #85830, #79724, #75110, #73091, #72038, #70542, #66949, #61863, #59972, #56287, #56166, #56156, #55471

@hauntsaninja
Copy link
Contributor

Okay, closing and will discuss on #56166

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2022
@abhii2003
Copy link
Author

okay that is good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-regex type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants