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