2012-12-19 6 views
5

내 목표는, 문자열과 장소 적 정수 또는 16 진수를 찾을 수 읽기와 그것을 대체하는 다른 문자열은 "[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

http://ideone.com/nMMNJm가 'R'(\ D + 0X [A-FA-F의 \의 D를] +) '를 이용하여 시도를 참조. – Blender

답변

1

귀하의 패턴은 진수와 진수 값을 구별하는 것을

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

같이해야한다. `정규식으로 |

+1

그것의 작업 thnx .. :) –

1

사용

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

+0

@DioF'\ d'는 소수를 처리합니다. – irrelephant

+0

맞아 ... 오늘 아침 일찍 왔어. –

관련 문제