Search code examples
regexhexbinary-search

Regex Match Hexidecimal in Groups of 2-8


I am working on a regular expression to match against a hexadecimal string and having some trouble near the end. I am specifically looking for groups of 2 bytes that do not contain 00 that are between 2 and 8 bytes long. I have it all working except that when there are less than 8 bytes, it will allow extra 00 to be in it sometimes.

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`

In match 1,2,5 there are extra 00, in match 3, it missed the 20 for some reason. If you have an idea what I missed, please let me know


Solution

  • You can avoid matching 00 by allowing only one 0 in two digits at a time instead:

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

    Demo: https://regex101.com/r/2hebvf/2