You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
re.sub does NOT substitute all the matching patterns when re.IGNORECASE is used #85830
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:
assignee=Noneclosed_at=<Date2020-08-29.22:32:24.365>created_at=<Date2020-08-29.21:47:33.268>labels= ['expert-regex', 'invalid', 'type-bug', '3.8']
title='re.sub does NOT substitute all the matching patterns when re.IGNORECASE is used'updated_at=<Date2020-08-29.22:32:24.364>user='https://bugs.python.org/anitrajpurohit28'
Usage of re flags leads to inconsistent results when
The pattern directly used in re.sub
The pattern is re.compile'd and used
Note 1: Input string is all in the lowercase 'all is fair in love and war'
Note 2: Results are always consistent in case of re.compile'd pattern
=======================================
The pattern directly used in re.sub
=======================================
>>> import re
>>> re.sub(r'[aeiou]', '#', 'all is fair in love and war')
'#ll #s f##r #n l#v# #nd w#r'
>>>
>>> re.sub(r'[aeiou]', '#', 'all is fair in love and war', re.IGNORECASE)
'#ll #s fair in love and war'
>>>
>>> re.sub(r'[aeiou]', '#', 'all is fair in love and war', re.IGNORECASE|re.DOTALL)
'#ll #s f##r #n l#v# #nd w#r'
>>>
>>>
=======================================
2. The pattern is re.compile'd and used
=======================================
>>> pattern = re.compile(r'[aeiou]', re.IGNORECASE)
>>> re.sub(pattern, '#', 'all is fair in love and war')
'#ll #s f##r #n l#v# #nd w#r'
>>>
>>> pattern = re.compile(r'[aeiou]')
>>> re.sub(pattern, '#', 'all is fair in love and war')
'#ll #s f##r #n l#v# #nd w#r'
>>>
>>> pattern = re.compile(r'[aeiou]', re.IGNORECASE| re.DOTALL)
>>> re.sub(pattern, '#', 'all is fair in love and war')
'#ll #s f##r #n l#v# #nd w#r'
Activity
anitrajpurohit28 commentedon Aug 29, 2020
Usage of re flags leads to inconsistent results when
Note 1: Input string is all in the lowercase 'all is fair in love and war'
Note 2: Results are always consistent in case of re.compile'd pattern
=======================================
=======================================
=======================================
2. The pattern is re.compile'd and used
=======================================
mrabarnett commentedon Aug 29, 2020
The 4th argument of re.sub is 'count', not 'flags'.
re.IGNORECASE has the numeric value of 2, so:
is equivalent to: