My aim is to read a string and where ever find an integer or hexadecimal number, replace that with "[0-9]" My string is:
a = hello word 123 with the 0x54673ef75e1a
a1 = hello word 123 with the 0xf
a2 = hello word 123 with the 0xea21f
a3 = hello word 123 with the 0xfa
Have tried with following:
b = re.sub(r"(\d+[A-Fa-f]*\d+[A-Fa-f]*)|(\d+)","[0-9]",a)
Get the following output:
hello word [0-9] with the [0-9]x[0-9]a
hello word [0-9] with the [0-9]xf
hello word [0-9] with the [0-9]xea[0-9]
hello word [0-9] with the [0-9]xfa
But the output should be like that:
hello word [0-9] with the [0-9]
hello word [0-9] with the [0-9]
hello word [0-9] with the [0-9]
hello word [0-9] with the [0-9]
Your pattern should be something like
b = re.sub(r"(0x[a-fA-F0-9]+|\d+)","[0-9]",a)
to distinguish between hex and decimal values.
Use
re.sub(r"(0x[\da-fA-F]+)|(\d+)","[0-9]",a)
See http://ideone.com/nMMNJm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With