我尝试在ascii中混合的字符串中查找消息,其格式如下:
"212zp* ��� �91238902Pressure_1,9472,Pressure_2,4251,is_usb: {0}478212zp+ ��� �91238902Pressure_1,9472,Pressure_2,4251,is_usb: {0}478212zp, ��� �91238902Pressure_1,9472,Pressure_2,4251,is_usb: {0}478212zp- ��� "
我需要解码hex和ascii消息。消息介于91238902
和478212
我怎样才能做到这一点?该字符串是动态的,可以包含未定义数量的邮件,也可能会出现标题91238902
和478212
中的错误
答案 0 :(得分:2)
这似乎是正则表达式的完美用法。这是指向pythex的链接:
import re
x = "212zp*91238902Pressure_1,9472,Pressure_2,4251,is_usb:{0}478212zp+91238902Pressure_1,9472,Pressure_2,4251,is_usb: {0}478212zp,91238902Pressure_1,9472,Pressure_2,4251,is_usb: {0}478212zp-"
# will grab between 91238902 and 478212
# alternative regex is: (?<=91238902).*?(?=478212)
result = re.findall(r'91238902(.*?)478212', x)
输出是一个列表:
['Pressure_1,9472,Pressure_2,4251,is_usb:{0}', 'Pressure_1,9472,Pressure_2,4251,is_usb: {0}', 'Pressure_1,9472,Pressure_2,4251,is_usb: {0}']