-1

I am trying to create a regex to match any of a list of hex commands e.g.

\x0a\x0b or \x76\x69or \x5e\x6a\x56\x02 ...

My initial approach was to use:

re.compile(b"|".join([hex_list])

However, as I do not know what the hex strings contain until the program is executed this can cause a problem when the hex code for special regex characters such as ( (ascii code\x28) are passed in.

What I am looking for is something akin to the raw string which will allow me to strictly match the characters themselves.

Pioneer_11
  • 670
  • 4
  • 19

1 Answers1

0

Use re.escape() to escape a literal string you want to use as/in a pattern:

re.compile(b"|".join([re.escape(h) for h in hex_list])
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206