在字符串中找不到匹配项,但应该有b

2024-09-29 21:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个奇怪的问题: 我有来自串行端口的字节数据。 我用以下方法解码:

data.decode("utf8","backslashreplace")

输出如下所示:

\xf028B       |\x80 10:00p@ @+\x81   :0000  

就像我想要的那样。 但是当我过滤正则表达式时(在regexr.com)你知道吗

data = re.search('(?<=\\x80).{10}?',data)

只是找不到匹配的。输出为“无”。你知道吗

我错过什么了吗?你知道吗


Tags: 数据方法端口recomdata字节utf8
1条回答
网友
1楼 · 发布于 2024-09-29 21:45:03

使用您的代码,它会产生您期望的结果。比较:

[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import re
>>> print(sys.version)
3.7.0 (default, Aug 22 2018, 20:50:05) 
[GCC 5.4.0 20160609]
>>> data = '\xf028B       |\x80 10:00p@ @+\x81   :0000'
>>> pattern = '(?<=\\x80).{10}?'
>>> print(re.search(pattern, data).group())
 10:00p@ @
>>> 

屏幕盖: screencap 如果您运行的是Python3.7windows,可能会有所不同?

更新:也适用于在Windows 10 64位上运行的Python3.7.0:

(53734290) C:\Users\daedw\Stackoverflow\53734290>Scripts\python.exe
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import re
>>> print(sys.version)
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
>>> data = '\xf028B       |\x80 10:00p@ @+\x81   :0000'
>>> pattern = '(?<=\\x80).{10}?'
>>> print(re.search(pattern, data).group())
 10:00p@ @
>>>

windows屏幕帽: enter image description here

相关问题 更多 >

    热门问题