My aim is to read a string and where ever find an integer or hexadecimal number, replace that with "[0-9]" My string is:
我的目标是读取一个字符串,并找到一个整数或十六进制数字,用“[0-9]”替换它我的字符串是:
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]
2 个解决方案
#1
1
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.
区分十六进制和十进制值。
#1
1
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.
区分十六进制和十进制值。