2012-12-19 26 views
5

Amacım, bir dize ve nereye kadar bir tamsayı veya onaltılık sayısını bulmak okumak o değiştirmektir diğer bazı dizeleri "[0-9]" Benim dizesidir:onaltılık için düzenli ifade ve

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) 

aşağıdaki çıktıyı alın:

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 

Ama outpu aşağıdaki denediniz Böyle davranma olmalıdır:

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

http://ideone.com/nMMNJm 'r' (\ d + 0 x [A-fa-f \ d] +) kullanılarak deneyin bakınız. – Blender

cevap

1

Sizin desen onaltılık ve ondalık değerler arasında ayrım şey

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

gibi olmalıdır. `Düzenli ifade olarak |

+1

onun çalışma thnx .. :) –

1

kullanımlar

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

+0

@DioF '\ d' ondalıklarını işliyor. – irrelephant

+0

Doğru ... Bu sabah erkenden. –