2012-12-19 5 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

は' R '(\ D + 0X [A-FA-Fの\ dの] +)' を使用してみてくださいを参照してください。 – Blender

答えて

1

あなたのパターンが進と小数の値を区別するために

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

のようなものでなければなりません。正規表現として `|

+1

は動作しています。:) –

関連する問題