为了在列表元素中捕捉文件的主名称,我在regex上卡住了。假设我有一个文件路径列表:
path_list = ['/Users/buggylines/histogram/offline-deployer-list_b7bacc7fdb-0e0e08077c_GERONIMO-2886_635_histogrambuglines_635.diff',
'/Users/buggylines/histogram/normal.jsp_aa0c2c26dd-90188cc2a4_GERONIMO-4597_1293_histogrambuglines_1293.diff',
'/Users/buggylines/histogram/hbase-env.sh_aa0c2c26dd-90188cc2a4_GERONIMO-4597_1293_histogrambuglines_1293.diff',
'/Users/buggylines/histogram/LICENSE-tesh_cd1ec17e43-4ebc5e8021_GERONIMO-5702_1573_histogrambuglines_785.diff',
'/Users/buggylines/histogram/geronimo_dcce59ae71-8f5c1aa7a1_GERONIMO-5661_1554_histogrambuglines_54.diff',
'/Users/buggylines/histogram/catalina-6.0.18-G678601.jar.sha1_cd1ec17e43-4ebc5e8021_GERONIMO-5702_1573_histogrambuglines_785.diff',
'/Users/buggylines/histogram/geronimo-naming-1.0.xsd_544dee5179-40a2ae1d41_GERONIMO-1027_131_histogrambuglines_131.diff',
'/Users/buggylines/histogram/6.0.18-G678601.README.TXT_cd1ec17e43-4ebc5e8021_GERONIMO-5702_1573_histogrambuglines_785.diff'
]
我只想使用regex
捕获文件名。我需要以下输出:
expected_output = ['offline-deployer-list',
'normal.jsp',
'hbase-env.sh',
'LICENSE-tesh',
'geronimo',
'catalina-6.0.18-G678601.jar.sha1',
'geronimo-naming-1.0.xsd',
'6.0.18-G678601.README.TXT'
]
这是我写的代码:
filename = []
for z, path in enumerate(path_list):
pattern = re.search("((?:\w+[-]\w+[-]\w+|\w+[-]\w+|\w+)[.]\w+[_])|(?<=histogram/)(?:\w+[-]\w+[-]\D+[_]|\D+[-]\w+[_])|(?<=histogram\/)(\w+[_])(?<=[_])", path)
pattern = pattern.groups()
filename.append(pattern[0])
但是,输出不是我所期望的。这是代码的输出:
filename = [None,
'normal.jsp_',
'hbase-env.sh_',
None,
None,
'jar.sha1_',
'0.xsd_',
'README.TXT_']
我需要人帮忙修理正则表达式。非常感谢。你知道吗
有一个对我有用:
在这里检查 https://regex101.com/r/VfQIJC/4
更新
一个更通用的匹配在最后一个/和第一个之后的是:
https://regex101.com/r/VfQIJC/5
你可以用操作系统路径基名称像这样:
输出:
输出:
相关问题 更多 >
编程相关推荐