24

I'm trying to perform a regex substitution on a bytes variable but I receive the error

  sequence item 0: expected a bytes-like object, str found

Here is a small code example to reproduce the problem with python3:

import re

try:
    test = b'\x1babc\x07123'
    test = re.sub(b"\x1b.*\x07", '', test)
    print(test)
except Exception as e:
    print(e)
1
  • Note: the * quantifier is greedy: it tries to match as much as possible. For example, provided your bytestring is \x1babc\x07123\x07 the result after substitution will be empty string. Adding ? after the quantifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. (from docs.python.org/3/library/re.html) Commented Sep 17, 2023 at 7:38

2 Answers 2

33

When acting on bytes object, all arguments must be of type byte, including the replacement string. That is:

test = re.sub(b"\x1b.*\x07", b'', test)
Sign up to request clarification or add additional context in comments.

Comments

7

Your replacement value must be a bytes object too:

re.sub(b"\x1b.*\x07", b'', test)
#                     ^^^

You can't replace matching bytes with a str object, even if that is an empty string object.

Demo:

>>> import re
>>> test = b'\x1babc\x07123'
>>> re.sub(b"\x1b.*\x07", b'', test)
b'123'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.