[New-bugs-announce] [issue41664] re.sub does NOT substitute all the matching patterns when re.IGNORECASE is used
Anit Rajpurohit
report at bugs.python.org
Sat Aug 29 17:47:33 EDT 2020
New submission from Anit Rajpurohit <anitrajpurohit28 at gmail.com>:
Usage of re flags leads to inconsistent results when
1. The pattern directly used in re.sub
2. 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
=======================================
1. 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'
----------
components: Regular Expressions
messages: 376083
nosy: anitrajpurohit28, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: re.sub does NOT substitute all the matching patterns when re.IGNORECASE is used
type: behavior
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41664>
_______________________________________
More information about the New-bugs-announce
mailing list