有一个字符串,我试图从符号之间提取值,但符号或分隔符也恰好是字符串的一部分。假设下面的字符串:message =': :1:1st message:2a:2nd message:x:this is where it fails status: fail :3:3rd message'和想要的结果:['1st message','2nd message','this is where it fails status: fail','3rd message']当前代码和结果:import redef trans(text): text = text+':' tag = re.findall(r':(.*?):',text) return [i for i in tag if not i.isspace()]trans(message)>>['1st message', '2nd message', 'this is where it fails status', '3']知道如何形成我的正则表达式以包含'status: fail '作为结果一部分的模式吗?
3 回答

四季花海
TA贡献1811条经验 获得超5个赞
您可以使用
re.findall(r'(?<=:\S:).+?(?=\s*:.:|$)', message)
后视冒号内的字符(或字符串的开头),然后匹配并延迟重复任何字符,直到先行看到冒号内的另一个字符(或字符串的结尾)。
输出:
['1st message', '2nd message', 'this is where it fails status: fail', '3rd message']
添加回答
举报
0/150
提交
取消