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

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀