classification
Title: re.I does not work as expected
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder: re.sub confusion between count and flags args
View: 11957
Assigned To: Nosy List: Magesh Kumar, ezio.melotti, mrabarnett
Priority: normal Keywords:

Created on 2016-02-13 00:19 by Magesh Kumar, last changed 2016-02-15 22:11 by Magesh Kumar. This issue is now closed.

Messages (9)
msg260216 - (view) Author: Magesh Kumar (Magesh Kumar) Date: 2016-02-13 00:19
I am in the process of re.sub the tag <item type="dict"> </item> with empty string from a xml output line.

If "re.I" is used, I am not able to remove the complete tag. 

========================================================================
>>> a
'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><item type="dict"><type type="str">MulticastClient</item><is'

>>> b = re.sub('\<\/?item(\s+type="dict")?\>', '', a, re.I)
>>> b
'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><aggregate type="list"><type type="str">MulticastClient</item><is'
>>> b = re.sub('\<\/?item(\s+type="dict")?\>', '', a)
>>> b
'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><aggregate type="list"><type type="str">MulticastClient<is'

========================================================================
msg260219 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2016-02-13 00:43
The 4th argument of re.sub is the count, not the flags.

Not a bug.
msg260220 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-02-13 00:44
See #11957
msg260328 - (view) Author: Magesh Kumar (Magesh Kumar) Date: 2016-02-15 19:14
Thanks for the inputs, It would be of great help, if someone could help me in explaining the below output :
========================================================================
>>> a 
'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><root><type type="str">MulticastClient</root><is'
>>> b = re.sub('\</?root\>', '', a, re.I)
>>> b
'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><type type="str">MulticastClient<is'
========================================================================
msg260329 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2016-02-15 19:28
The pattern '\</?root\>', which is the same as '</?root>', matches the string '<root>', and that is replaced with ''.
msg260330 - (view) Author: Magesh Kumar (Magesh Kumar) Date: 2016-02-15 19:39
:-)
Thanks a lot Matthew for the inputs.

If we compare the first example (<item>) and the <root> example, I am using re.I as the third element. But for the <root> example, still I am not to get the substitution happening correctly.

Could you pls, let me know the reason of change in behaviour.
msg260331 - (view) Author: Magesh Kumar (Magesh Kumar) Date: 2016-02-15 19:40
Corrected Message :


If we compare the first example (<item>) and the <root> example, I am using re.I as the third element. But for the <root> example, still I am able to get the substitution happening correctly.

Could you pls, let me know the reason of change in behaviour.
msg260334 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2016-02-15 21:13
The 3rd argument is the count (the maximum number of replacements, although 0 means no limit, not no replacements). You're passing in the flag re.I instead. re.I happens to have the numeric value 2, so you're telling it to do no more than 2 replacements.
msg260335 - (view) Author: Magesh Kumar (Magesh Kumar) Date: 2016-02-15 22:11
Thanks Matthew. :-)
History
Date User Action Args
2016-02-15 22:11:04Magesh Kumarsetmessages: + msg260335
2016-02-15 21:13:54mrabarnettsetmessages: + msg260334
2016-02-15 19:40:28Magesh Kumarsetmessages: + msg260331
2016-02-15 19:39:26Magesh Kumarsetmessages: + msg260330
2016-02-15 19:28:14mrabarnettsetmessages: + msg260329
2016-02-15 19:14:58Magesh Kumarsetmessages: + msg260328
2016-02-13 00:44:14ezio.melottisetstatus: open -> closed
superseder: re.sub confusion between count and flags args
messages: + msg260220

resolution: not a bug
stage: resolved
2016-02-13 00:43:01mrabarnettsetmessages: + msg260219
2016-02-13 00:19:24Magesh Kumarcreate