正则表达式以2-8为一组匹配十六进制

时间:2018-09-25 04:00:46

标签: regex hex binary-search

我正在研究一个正则表达式以匹配十六进制字符串,并且在结尾处遇到了一些麻烦。我专门在寻找2个字节的组,其中不包含长度在2到8个字节之间的00。我有所有的工作,除了当少于8个字节时,有时它会允许额外的00。

https://regex101.com/r/jq3QpP/1/

(?!(00)+)([0-9a-fA-F]{2,8})?(?!(00)+)    // This on the below text gives the following matches


C86B0200554E0200C86B02000000000000000000270000008109000000000000EC6A050079750
18881000000410000280100000000000000000001000002010400000000000000000000000000
0000000000000000000000F65FA45900000000FF0000002F0000000000000049000000403C9F5
A000000000000000000000000FFFF330000000000000F06EAE8333536

Match 1
Full match  0-8 `C86B0200`
Group 2.    0-8 `C86B0200`
Match 2
Full match  8-16    `554E0200`
Group 2.    8-16    `554E0200`
Match 3
Full match  16-21   `C86B0`
Group 2.    16-21   `C86B0`
Match 4
Full match  21-21   ``
Match 5
Full match  39-47   `02700000`
Group 2.    39-47   `02700000`

在比赛1,2,5中有额外的00,在比赛3中,由于某种原因错过了20。如果您知道我错过了什么,请告诉我

1 个答案:

答案 0 :(得分:0)

通过一次只允许两位数字中的一个00来避免匹配0

(?:[A-F1-9][A-F0-9]|[A-F0-9][A-F1-9]){1,4}(?=(?:..)*$)

演示:https://regex101.com/r/2hebvf/2