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 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型