正则表达式为45个十六进制数或48个十六进制数的模式 – Python

我的文件包含45个十六进制数字,由空格分隔或48个十六进制数字,由空格分隔.我个人而不是整体需要所有这些数字.我目前正在使用蛮力方法获得45个数字.

pattern = re.compile("([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s")

然而,即便如此,我仍然无法弄清楚如何在48十六进制数字实例中提取剩余的三个数字.你能帮我解决这个问题吗?

我会避免像下面的解决方案(如果它工作,没有尝试过),因为我将不得不为每个实例拆分字符串,即考虑它给出正确的输出!

(((?:[0-9a-f]{2})\s){48})|(((?:[0-9a-f]{2})\s){45})

谢谢!

最佳答案
在编写长RE时,请考虑使用re.VERBOSE使其更具可读性.

pattern = re.compile(r"""
 ^( [0-9a-fA-F]{2} (?: \s [0-9a-fA-F]{2} ){44}
                (?:(?: \s [0-9a-fA-F]{2} ){3} )? )$
""", re.VERBOSE)

读为:两个十六进制数字,后跟44次(空格后跟两个十六进制数字),可选地后跟3次(空格后跟两个十六进制数字).

测试:

>>> pattern.match(" ".join(["0f"] * 44))
>>> pattern.match(" ".join(["0f"] * 45))
<_sre.SRE_Match object at 0x7fd8f27e0738>
>>> pattern.match(" ".join(["0f"] * 46))
>>> pattern.match(" ".join(["0f"] * 47))
>>> pattern.match(" ".join(["0f"] * 48))
<_sre.SRE_Match object at 0x7fd8f27e0990>
>>> pattern.match(" ".join(["0f"] * 49))

最后,要检索各个数字,请对匹配结果执行.group(0).split().这比编写将所有数字放入不同组的RE要容易得多.

编辑:好的,这是如何解决原始问题.只需动态构建RE.

chunk = r"""([0-9a-fA-F]{2}\s)"""
pattern = re.compile(chunk * 45 + "(?:" + chunk * 3 + ")?")

转载注明原文:正则表达式为45个十六进制数或48个十六进制数的模式 – Python - 代码日志