使用regex在十六进制数据中查找模式,但得到重复项

2024-06-28 10:56:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个regexpython脚本来检查Hex数据并找到如下的模式

r"(.{6,}?)\1{2,}"

它所做的就是寻找至少6个字符长的十六进制字符串重复,至少有两个实例重复。我的问题是,它还在已经找到的较大字符串中找到子字符串,例如: 如果它是“A00B00A00B00A00B00A00A00B00A00B00A00B00”,它会找到2个“a00b00a00b00a00b00”实例和6个“a00b00”实例,我怎么能只保留找到的最长模式,而忽略甚至没有更多硬编码参数的较短模式


Tags: 数据实例字符串脚本编码参数模式hex
2条回答
#!/usr/bin/python

import fnmatch

pattern_string = "abcdefabcdef"

def print_pattern(pattern, num):
    n = num

    # takes n and splits it by that value in this case 6
    new_pat = [pattern[i:i+n] for i in range(0, len(pattern), n)]
    # this is the hit counter for matches
    match = 0
    # stores the new value of the match
    new_match = ""
    #loops through the list to see if it matches more than once
    for new in new_pat:
        new_match = new
        print new
        #if matches previous keep adding to match
        if fnmatch.fnmatch(new, new_pat[0]):
            match += 1
    if match:
        print "Count: %d\nPattern:%s" %(match, new_match)
        #returns the match
        return new_match


print_pattern(pattern_string, 6)

正则表达式更好,但写起来更有趣

相关问题 更多 >