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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew