Consider the following execution.
Python 2.7.2 (default, Sep 19 2012, 01:44:39)
[GCC 4.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> old_value=1
>>> new_value=2
>>> re.sub('0x(..)'+format(old_value, '02x'), '0x\\1x'+format(new_value, '02x'), '0xab01')
'0xabx02'
>>> re.sub('0x(..)'+format(old_value, '02x'), '0x\\1'+format(new_value, '02x'), '0xab01')
'0xB'
So Basically, I am trying to modify a hexadecimal value, but only the two least significant digits. In this example, I try to substitute '01' with '02', if present in input string. The output of first re.sub() call is expected. However, I completely fail to understand the output of the second call (which is ofcourse, I want to do). This is what puzzling me for a long time now and I tend to ask that could it be a python bug? OR am I missing something here?