Negative look at python regex

I would like the regex to match a sequence of bytes when the string '02 d0 'does not occur at a specific position in the string. The position in which this line of two bytes cannot be executed is the positions of bytes 6 and 7, starting from the 0th byte on the right side.

This is what I used for testing:

#!/usr/bin/python import re p0 = re.compile('^24 [\da-f]{2} 03 (01|03) [\da-f]{2} [\da-f]{2} [\da-f]{2} (([^0])| (0[^2])|(02 [^d])|(02 d[^0])) 01 c2 [\da-f]{2} [\da-f]{2} [\da-f]{2} 23') p1 = re.compile('^24 [\da-f]{2} 03 (01|03) [\da-f]{2} [\da-f]{2} [\da-f]{2} (([^0])|(0[^2])|(02 [^d])|(02 d[^0])) 01') p2 = re.compile('^24 [\da-f]{2} 03 (01|03) [\da-f]{2} [\da-f]{2} [\da-f]{2} (([^0])|(0[^2])|(02 [^d])|(02 d[^0]))') p3 = re.compile('^24 [\da-f]{2} 03 (01|03) [\da-f]{2} [\da-f]{2} [\da-f]{2} (?!02 d0) 01') p4 = re.compile('^24 [\da-f]{2} 03 (01|03) [\da-f]{2} [\da-f]{2} [\da-f]{2} (?!02 d0)') yes = '24 0f 03 01 42 ff 00 04 a2 01 c2 00 c5 e5 23' no = '24 0f 03 01 42 ff 00 02 d0 01 c2 00 c5 e5 23' print p0.match(yes) # fail print p0.match(no) # fail print '\n' print p1.match(yes) # fail print p1.match(no) # fail print '\n' print p2.match(yes) # PASS print p2.match(no) # fail print '\n' print p3.match(yes) # fail print p3.match(no) # fail print '\n' print p4.match(yes) # PASS print p4.match(no) # fail 

I looked through this example , but this method is less restrictive than I need. Can someone explain why I can only fit correctly when a negative look is ahead at the end of the line? What do I need to do to match when '02 d0 'does not occur in this particular bit position?

+7
source share
1 answer

Lookaheads are "zero width", which means they do not consume any characters. For example, these two expressions will never match:

  • (?=foo)bar
  • (?!foo)foo

To make sure the number is not a specific number, you can use:

 (?!42)\d\d # will match two digits that are not 42 

In your case, it might look like this:

 (?!02)[\da-f]{2} (?!0d)[\da-f]{2} 

or

 (?!02 d0)[\da-f]{2} [\da-f]{2} 
+11
source

All Articles