简体   繁体   中英

Regex python expression

I'm trying to read a binary file.

My objective is to find all the matches of "10, 10, [any hex value exactly one time], either EE or DD]"

Thought I could do it like this:

pattern = (b"\x10\x10\[0-9a-fA-F]?\[xDD|xEE]")

Clearly not working. It seems that it becomes an error at the third part. I tried dissecting the statement and x10 and x11 works, but the rest just won't.

My understanding of "[0-9a-fA-F]?" is that it matches the range in the brackets 0 or 1 times. and the third part "xDD or xEE" am I wrong?

Any ideas?

Use the regex

b'\x10\x10.[\xdd\xee]'

A single . matches any character (any one-byte) single time, and a single [ab] matches a or b a single time.


>>> re.match(b'\x10\x10.[\xdd\xee]', b'\x10\x10\x00\xee')
<_sre.SRE_Match object; span=(0, 4), match=b'\x10\x10\x00\xee'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM