Reputation: 1289
I have a bytearray:
s = b'\x01\x80\x00\x04_\xa9\xa20\x01\x19\x00'
I want to search and find:
\xa9\xa20
Then once found I want to get the next 2 bytes after that. In this case:
\x01\x19
I've tried:
m = re.search(b'[(?:\xa9\xa20)]{2}',s,re.DOTALL).group(1)
But get an Index error:
m = re.search(b'[(?:\xa9\xa20)]{2}',s,re.DOTALL).group(1)
IndexError: no such group
Upvotes: 1
Views: 78
Reputation: 2123
Try this pattern:
\\xa9\\xa20(\\xa?\d+\\xa?\d+)
See Demo in Regex101
Upvotes: 0
Reputation: 627077
You have a corrupt regex pattern here since you put a (?:\xa9\xa20)
non-capturing group into a character class (repeated twice). However, just removing the square brackets won't help.
You can fix the code using
import re
s = b'\x01\x80\x00\x04_\xa9\xa20\x01\x19\x00'
m = re.search(b'\xa9\xa20(.{2})', s, re.DOTALL)
if m:
print(m.group(1)) # => b'\x01\x19'
See the Python demo
That is, rather than unsuccessfully trying to match two consecutive occurrences of \xa9\xa20
bytes, match a \xa9\xa20
byte sequence once and then match and capture any two bytes after them with (.{2})
capturing group.
Upvotes: 1