dimanche 28 juin 2015

python regex use capture group to define another groups length { }

I am parsing streaming hex data with python regex. I have the following packet structure that I am trying to extract from the stream of packets:

'\xaa\x01\xFF\x44'

  • \xaa - start of packet
  • \x01 - data length [value can vary from 00-FF]
  • \xFF - data
  • \x44 - end of packet

i want to use python regex to indicate how much of the data portion of the packet to match as such:

r = re.compile('\xaa(?P<length>[\x00-\xFF]{1})(.*){?P<length>}\x44')

this compiles without errors, but it doesnt work. I suspect it doesnt work because it the regex engine cannot convert the <length> named group hex value to an appropriate integer for use inside the regex {} expression. Is there a method by which this can be accomplished in python without resorting to disseminating the match groups?

Background: I have been using erlang for packet unpacking and I was looking for something similar in python

Aucun commentaire:

Enregistrer un commentaire