Skip to content

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 as not planned
@abhii2003

Description

@abhii2003

# 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions