duan97689 2019-01-29 14:03
浏览 108
已采纳

为什么这些按位正则表达式在golang中匹配不同?

Given this binary data and two regular expressions, why does golang match them differently?

var (
    data = []byte{0x03, 0x00, 0x00, 0x88, 0x02, 0xf0, 0x80, 0x72, 0x03, 0x00, 0x79, 0x20, 0xdd, 0x39, 0x22, 0x4d, 0xcf, 0x6d, 0x17, 0x29, 0x02, 0xee, 0xe3, 0x7f, 0x5e, 0xca, 0x17, 0x62, 0xc8, 0x56, 0x24, 0x01, 0x1e, 0x9f, 0xa0, 0x96, 0xc6, 0x4f, 0xbb, 0xa2, 0x51, 0x7b, 0xbf, 0x33, 0x31, 0x00, 0x00, 0x05, 0x4c, 0x00, 0x00, 0x1a, 0x6a, 0x00, 0x00, 0x03, 0x8d, 0x34, 0x00, 0x00, 0x00, 0x00, 0x04, 0x14, 0x81, 0xe7, 0x86, 0xa7, 0x76, 0x51, 0x02, 0x9d, 0x18, 0x09, 0xff, 0xde, 0xde, 0x05, 0x51, 0x02, 0x9d, 0x18, 0x0a, 0x89, 0xee, 0xf8, 0x81, 0x53, 0x51, 0x02, 0x9d, 0x18, 0x0b, 0x82, 0xce, 0xef, 0xad, 0x63, 0x51, 0x02, 0x9d, 0x18, 0x0c, 0x00, 0x00, 0x04, 0xe8, 0x89, 0x69, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x89, 0x6a, 0x00, 0x13, 0x00, 0x89, 0x6b, 0x00, 0x04, 0x00, 0x00, 0xb4, 0x62, 0x00, 0x00, 0x00, 0x00, 0x72, 0x03, 0x00, 0x00}

    regexA = regexp.MustCompile(`\x72\x03(?P<TLen>[\x00-\xFF]{2})(?P<Payload>[\x00-\xFF]+)`)
    regexB = regexp.MustCompile(`\x72\x03(?P<TLen>[\x00-\xFF]{2})(?P<Payload>.+)`)
)

Shouldn't regexA's [\x00-\xFF]+ match as good as regexB's .+?

I'm testing using this:

func main() {
    log.Printf("testing regexA")
    applyRegex(regexA)
    log.Printf("testing regexB")
    applyRegex(regexB)
}

func applyRegex(r *regexp.Regexp) {

    matches := r.FindAllSubmatch(data, -1)
    groups := r.SubexpNames()

    for mIdx, match := range matches {
        findings := smap{}
        for idx, submatch := range match {
            findings[groups[idx]] = fmt.Sprintf("% x", submatch)
        }
        log.Printf("match #%d: %+v", mIdx, findings)
    }
}

And getting this output:

2009/11/10 23:00:00 testing regexA
2009/11/10 23:00:00 match #0: map[:72 03 00 79 20 TLen:00 79 Payload:20]
2009/11/10 23:00:00 testing regexB
2009/11/10 23:00:00 match #0: map[:72 03 00 79 20 dd 39 22 4d cf 6d 17 29 02 ee e3 7f 5e ca 17 62 c8 56 24 01 1e 9f a0 96 c6 4f bb a2 51 7b bf 33 31 00 00 05 4c 00 00 1a 6a 00 00 03 8d 34 00 00 00 00 04 14 81 e7 86 a7 76 51 02 9d 18 09 ff de de 05 51 02 9d 18 TLen:00 79 Payload:20 dd 39 22 4d cf 6d 17 29 02 ee e3 7f 5e ca 17 62 c8 56 24 01 1e 9f a0 96 c6 4f bb a2 51 7b bf 33 31 00 00 05 4c 00 00 1a 6a 00 00 03 8d 34 00 00 00 00 04 14 81 e7 86 a7 76 51 02 9d 18 09 ff de de 05 51 02 9d 18]

Is parsing binary data using regex not an actual possibility?

Playground link: https://play.golang.org/p/gHWqeyPuPNJ

  • 写回答

1条回答 默认 最新

  • douxian3170 2019-01-29 15:58
    关注

    The regexp documentation states that:

    All characters are UTF-8-encoded code points.

    So I think the character class ranges will be unicode code points ranges and not byte ranges. Also the matched text is treated as UTF-8 so i'm not sure what will happen at data = []byte{... , 0xdd, ...}. It might be decoded as a code point greater than 0xff. So i'm not sure how well it will work to use the standard library regexp package to do binary matching.

    A side note (?P<Payload>.+) will not match all code points but (?P<Payload>(?s).+) will. s flag is:

    let . match (default false)

    Hope that helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来