1

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?

bbv
  • 501
  • 1
  • 5
  • 14

1 Answers1

4

'0x\\1'+format(new_value, '02x') is '0x\\102', and Python treats the \\102 as a single escape sequence. Because there are exactly three octal digits, it is treated as an octal escape (rather than a reference to a group 102), and octal 102 is the character B. To fix this, write '0x\\g<1>'+format(new_value, '02x').

jwodder
  • 54,758
  • 12
  • 108
  • 124