Python正则表达式以排除字符串的倾斜

2024-05-12 17:07:08 发布

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

给定以下字符串:

dpkg.log.looker.test.2019-09-25

我想提取:

looker.test 或者

looker。你知道吗


我已经尝试了多种组合,但没有一种真正只提取主机名。如果我试图过滤整个文件(dpkg.对数,它也会忽略后面的字符:
/[^dpkg.log].+(?=.[0-9]{4}-[0-9]{2}-[0-9]{2})/

返回:
er.test

有没有办法忽略整个字符串”dpkg.对数“不忽略后面重复的字符?你知道吗


Tags: 文件字符串testlog对数字符主机名er
1条回答
网友
1楼 · 发布于 2024-05-12 17:07:08

也许,下面的表达式可以与re.findall配合使用:

[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}

Demo

测试

import re

regex = r'[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}'
string = '''
dpkg.log.looker.test.2019-09-25
dpkg.log.looker.test1.test2.2019-09-25
'''

print(re.findall(regex, string))

输出

['looker.test', 'looker.test1.test2']

相关问题 更多 >