python的字节的正则

发布于 2025-01-24 21:09:30 字数 439 浏览 4 评论 0原文

我想在以下字节中提取10.00毫升:b'\ x0200s10.00ml \ x03' 因此,我尝试在200s和\ x03之间提取10.00毫升:

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

没有可行的元素:

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

仅使用字符串,我有一个最低的工作示例:

test_string = 'a3223b'
result = re.search('a(.*)b', test_string)
print(result.group(1))

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-01-31 21:09:30

您可以使用

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

请注意,\ x02\ x03是文本控制字符的启动和开始,因此您无法将其匹配为文字文本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文