Reputation: 407
pattern = b'\x08\x00\x2a\x00'
re.search(b'\x08', pattern).end()
>> 1
re.search(b'\x2a',pattern).end()
>> error: nothing to repeat at position 0
I am trying to search for a byte pattern in a dicom file, if it includes a hex value a-f, it gets an error.
Using str.find seems to work, but I was wondering if there is a way to make re serch work as well
Upvotes: 1
Views: 212
Reputation: 627292
The '\x2a'
is an escape sequence that represents a *
char. It is a quantifier that repeats the preceding pattern 0 or more times. You want to match a *
char, so you may either tell the regex engine to search for an escaped *
, b'\\\x2a'
, a regular string literal that represents a \*
literal string, or use a hex entity with a regex escape, rb'\x2a'
, a raw string literal that repesents a literal \x2a
string.
print(re.search(rb'\x2a',b'\x08\x00\x2a\x00').end()) # => 3
print(re.search(b'\\\x2a',b'\x08\x00\x2a\x00').end()) # => 3
See the Python 3 demo.
Upvotes: 3