2012-12-19 16 views
5

Moim celem jest, aby przeczytać ciąg i gdzie znaleźć całkowitą lub liczbę szesnastkową kiedykolwiek zastąpi że z „[0-9]” mój ciąg jest:Wyrażenie regularne na szesnastkowy i inne ciągi

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 

próbowałem z następujących powodów:

b = re.sub(r"(\d+[A-Fa-f]*\d+[A-Fa-f]*)|(\d+)","[0-9]",a) 

uzyskać następujące dane wyjściowe:

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 

ale outpu t powinno być tak:

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

Użyj 'R '(0 x [A-Fa, F \ d] + | \ D +)'' jako regexpa. – Blender

Odpowiedz

1

Twój wzór powinno być coś jak

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

odróżnić hex i wartości dziesiętnych.

+1

jego działanie thnx .. :) –