5

I would like to extract 10.00ML in following byte: b'\x0200S10.00ML\x03' So I've tried extracting the 10.00ML between 200S and \x03:

result = re.search(b'200S(.*)x03', b'\x0200S10.00ML\x03')

which didn't work, no element was found:

AttributeError: 'NoneType' object has no attribute 'group'

Using only strings I have a minimum working example:

test_string = 'a3223b'
result = re.search('a(.*)b', test_string)
print(result.group(1))
1
  • 2
    The characters x03 do not actually appear in that string, that's just part of an escape sequence. You would need the preceding backslash as well. Commented Apr 28, 2022 at 13:30

1 Answer 1

5

You can use

import re
text = b'\x0200S10.00ML\x03'
m = re.search(rb'\x0200S(.*?)\x03', text, re.S)
if m:
    print( m.group(1).decode('utf-8') )

# => 10.00ML

Note that \x02 and \x03 are START OF HEADING and START OF TEXT control chars, so you cannot match them as literal text.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.