Marco Bobinger
Marco Bobinger

Reputation: 165

Regex on bytes in Python

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))

Upvotes: 4

Views: 2647

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627056

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.

Upvotes: 5

Related Questions