简体   繁体   English

Python正则表达式不匹配。 我不能应付

[英]Python regex does not match. I cant handle it

why this match does not work ? 为什么这场比赛不起作用? The problem is with D 问题出在D

import re

A = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
B = [0xa8, 0x2c, 0x53, 0x20, 0xca, 0x62, 0x49, 0x13]
C = [0x1A, 0xC4, 0x17, 0x05, 0x47, 0xE8, 0xA3, 0x83]
D = [0x81, 0x63, 0x1f, 0x55, 0xdb, 0x18, 0x2a, 0xab]


for bin_header in [A, B, C, D]:
    bin_str = ''.join(map(chr, bin_header))
    r = re.match(bin_str, bin_str)
    if not r:
        print map(hex, bin_header) # Surprise it prints D

You are trying to match a string with a regular expression, but ignoring the regular expression syntax. 您正在尝试将字符串与正则表达式匹配,但忽略了正则表达式语法。 Here is your problem: 这是您的问题:

>>> chr(0x2a)
'*'

The * has special meaning in regular expression syntax. *在正则表达式语法中具有特殊含义。 "abc*" the regular expression won't match "abc*" the string (it will match "abcccc" for example). "abc*"正则表达式与字符串"abc*"不匹配(例如,它将与"abcccc"匹配)。

I suggest you use = or x in y not re.match for this. 我建议您为此使用=x in y不是re.match Passing random bytes to a regular expression isn't a great idea if you don't know that they are all valid characters, or even what the characters are. 如果您不知道它们都是有效字符,甚至不知道这些字符是什么,那么将随机字节传递给正则表达式并不是一个好主意。

Here's an example using in : 下面是使用的例子in

import re

A = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
B = [0xa8, 0x2c, 0x53, 0x20, 0xca, 0x62, 0x49, 0x13]
C = [0x1A, 0xC4, 0x17, 0x05, 0x47, 0xE8, 0xA3, 0x83]
D = [0x81, 0x63, 0x1f, 0x55, 0xdb, 0x18, 0x2a, 0xab]


for bin_header in [A, B, C, D]:
    bin_str = ''.join(map(chr, bin_header))
    matches =  bin_str in bin_str
    if not matches:
        print map(hex, bin_header) # Matches all examples.

Even so, constructing strings from unknown byte streams won't play nicely with character encodings, and you should use the correct methods for working with byte sequences. 即使这样,从未知字节流构造字符串也不能很好地与字符编码一起使用,并且您应该使用正确的方法来处理字节序列。

If you really want to use strings for this, you could represent them as hex strings. 如果您确实想为此使用字符串,则可以将它们表示为十六进制字符串。 As hex strings are only 0-9a-z you could safely use any string or regular expression matching etc. 由于十六进制字符串仅为0-9a-z您可以安全地使用任何字符串或正则表达式匹配等。

for bin_header in [A, B, C, D]:
    bin_str = ''.join('%02x' % i for i in bin_header)
    matches =  bin_str in bin_str
    print(bin_str, matches)
    if not matches:
        print map(hex, bin_header)

gives

('ffffffffffffffff', True)
('a82c5320ca624913', True)
('1ac4170547e8a383', True)
('81631f55db182aab', True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 说我的日期格式不匹配。 另外,如何将这些日期格式化为年/月/日(python) - Says that I date formatting does not match. Also, how can I format these dates into year/month/date (python) 正则表达式-匹配后查找后续行。 使用pythex,但不能在脚本中使用(使用python) - regex - find subsequent lines after a match. Work with pythex but not in script (with python) 首先预测然后在前瞻匹配后寻找最接近的匹配捕获组。 Python中的RegEx - First lookahead then look for closest matching capture group behind the lookahead match. RegEx in Python 我的正则表达式错过了我要匹配的项目之一。 有什么我想念的吗? - My regex formula misses one of the items I am trying to match. Is there something I am missing? Python正则表达式不匹配 - Python regex does not match Python 不匹配正则表达式检查 - Python does not match regex check 正则表达式与python不匹配 - Regex does not match according to python 比较两个列表中的元素,如果匹配则删除元素。 python - Comparing elements in two lists and removing elements if match. python Python 正则表达式:为什么我的模式不匹配? - Python Regex: Why my pattern does not match? 为什么Python regex在这里不匹配(?=)? - Why the Python regex does not match for (?=) here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM