我的目的是讀取一個字符串,並在不斷尋找一個整數或十六進制數,替換成「[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
試圖與以下:
b = re.sub(r"(\d+[A-Fa-f]*\d+[A-Fa-f]*)|(\d+)","[0-9]",a)
得到下面的輸出:
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
但outpu T應當是這樣的:
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]
嘗試使用的r '(0X [A-發-F \ d] + | \ d +)''爲正則表達式。 – Blender