gpt4 book ai didi

python - 十六进制和其他一些字符串的正则表达式

转载 作者:太空狗 更新时间:2023-10-30 01:29:45 25 4
gpt4 key购买 nike

我的目标是读取一个字符串,并在找到整数或十六进制数的地方将其替换为“[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

但输出应该是这样的:

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]

最佳答案

你的模式应该是这样的

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

区分十六进制和十进制值。

关于python - 十六进制和其他一些字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com