2012-12-19 30 views
5

我的目的是讀取一個字符串,並在不斷尋找一個整數或十六進制數,替換成「[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] 
+0

嘗試使用的r '(0X [A-發-F \ d] + | \ d +)''爲正則表達式。 – Blender

回答

1

你的模式應該是這樣的

b = re.sub(r"(0x[a-fA-F0-9]+|\d+)","[0-9]",a) 

以十六進制和十進制值之間進行區分。

+1

其工作thnx .. :) –

相關問題