通过正则表达式查找符号之间的值,其中符号可能是值的一部分

有一个字符串,我试图从符号之间提取值,但符号或分隔符也恰好是字符串的一部分。


假设下面的字符串:


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 re

def 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 '作为结果一部分的模式吗?


慕妹3146593
浏览 188回答 3
3回答

呼啦一阵风

尝试使用负前瞻:r'[^\s]:(.*?):(?!\s)。结果:['1st message', '2nd message', 'this is where it fails status: fail ', '3rd message'][^\s]是不匹配前面有空格字符的冒号,因此它修复3rd message.:(?!\s)是匹配一个冒号,后面没有空格字符,所以它修复了status: fail。换句话说,我添加的两部分都在要匹配的子字符串周围创建了一个边距,该边距不能由前面或后面跟有空格字符的冒号组成。

四季花海

您可以使用re.findall(r'(?<=:\S:).+?(?=\s*:.:|$)',&nbsp;message)后视冒号内的字符(或字符串的开头),然后匹配并延迟重复任何字符,直到先行看到冒号内的另一个字符(或字符串的结尾)。输出:['1st&nbsp;message',&nbsp;'2nd&nbsp;message',&nbsp;'this&nbsp;is&nbsp;where&nbsp;it&nbsp;fails&nbsp;status:&nbsp;fail',&nbsp;'3rd&nbsp;message']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python