python中十六进制字符串的正则表达式不起作用 [英] Regular expression for hexadecimal string in python not working
问题描述
我有一个正则表达式来匹配像这样的字符串:
I have a regular expression to match strings like:
-D2CBA65440D
--D2CBA65440D
-77094A27E09
--77094A27E09
-77094A27E
--77094A27E
-770
-77094A27E09-
--77094A27E09--
基本上,它匹配一个十六进制的字符串,该字符串被一个或多个换行符或空格包围,并且具有前缀-可以也可以不具有-作为后缀
basically, it matches a hexadecimal string surrounded by one or more line breaks or white space, and has the prefix -- and may or may not have -- as suffix
我使用以下python代码,并且在大多数情况下都可以正常工作:
i use the following python code, and it works fine most of the time:
hexaPattern = "\s--[0-9a-fA-F]+[--]?\s"
hex = re.search(hexaPattern, part)
if hex:
print "found a match"
这对以上所有方法均有效,但在此块中与--77094A27E09不匹配:
this works for all of the above but it doesn't match --77094A27E09 in this block:
<div id="arrow2" class="headerLinksImg" style="display:block
--77094A27E09
;">
但匹配以下内容中的相同字符串:
but matches the same string in:
<input type="checkbox" name="checkbox" id="checkboxKG3" class
--77094A27E09
Content-T="checkboxKG" value="KG3" />
我在做什么错了?
推荐答案
我使用了以下内容:
pattern = re.compile(r'(\n--)([0-9A-F]+)(--)?', re.I | re.S | re.M)
,效果很好.感谢您的所有贡献.
and it worked fine. Thanks to all your contributions.
这篇关于python中十六进制字符串的正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!