import re
names = ['g18_84pp_2A_MVP1_GoodiesT0-HKJ-DFG_MIX-CMVP1_Y1000-MIX.txt',
'g18_84pp_2A_MVP2_GoodiesT0-HKJ-DFG_MIX-CMVP2_Y1000-MIX.txt',
'g18_84pp_2A_MVP3_GoodiesT0-HKJ-DFG_MIX-CMVP3_Y1000-MIX.txt',
'g18_84pp_2A_MVP4_GoodiesT0-HKJ-DFG_MIX-CMVP4_Y1000-MIX.txt',
'g18_84pp_2A_MVP5_GoodiesT0-HKJ-DFG_MIX-CMVP5_Y1000-MIX.txt',
'g18_84pp_2A_MVP6_GoodiesT0-HKJ-DFG_MIX-CMVP6_Y1000-MIX.txt',
'g18_84pp_2A_MVP7_GoodiesT0-HKJ-DFG_MIX-CMVP7_Y1000-MIX.txt']
f = lambda x: re.findall('g18_84pp_2A_MVP(.*?)_GoodiesT0(.*?)_MIX(.*?)\.txt', x)
for x in names:
print(f(x))
生产
[('1', '-HKJ-DFG', '-CMVP1_Y1000-MIX')]
[('2', '-HKJ-DFG', '-CMVP2_Y1000-MIX')]
[('3', '-HKJ-DFG', '-CMVP3_Y1000-MIX')]
[('4', '-HKJ-DFG', '-CMVP4_Y1000-MIX')]
[('5', '-HKJ-DFG', '-CMVP5_Y1000-MIX')]
[('6', '-HKJ-DFG', '-CMVP6_Y1000-MIX')]
[('7', '-HKJ-DFG', '-CMVP7_Y1000-MIX')]
过滤与此模式不匹配的名称:
names = list(filter(f, names))
由于尚不清楚您要做什么,因此这将是一个很好的起点。
更新
问题已更新。这是您(可能)想要实现的目标:
import re
names = ['g18_84pp_2A_MVP1_GoodiesT0-HKJ-DFG_MIX-CMVP1_Y1000-MIX.txt',
'g18_84pp_2A_MVP2_GoodiesT0-HKJ-DFG_MIX-CMVP2_Y1000-MIX.txt',
'g18_84pp_2A_MVP3_GoodiesT0-HKJ-DFG_MIX-CMVP3_Y1000-MIX.txt',
'g18_84pp_2A_MVP4_GoodiesT0-HKJ-DFG_MIX-CMVP4_Y1000-MIX.txt',
'g18_84pp_2A_MVP5_GoodiesT0-HKJ-DFG_MIX-CMVP5_Y1000-MIX.txt',
'g18_84pp_2A_MVP6_GoodiesT0-HKJ-DFG_MIX-CMVP6_Y1000-MIX.txt',
'g18_84pp_2A_MVP7_GoodiesT0-HKJ-DFG_MIX-CMVP7_Y1000-MIX.txt']
expression = 'g18_84pp_2A_MVP(.*?)_Goodies(.*?)_MIX(.*?)\.txt'
f = lambda x: re.findall(expression, x)
_f = lambda x: len(re.findall(expression, x))==3
for x in names:
print(f(x))
输出
[('1', 'T0-HKJ-DFG', '-CMVP1_Y1000-MIX')]
[('2', 'T0-HKJ-DFG', '-CMVP2_Y1000-MIX')]
[('3', 'T0-HKJ-DFG', '-CMVP3_Y1000-MIX')]
[('4', 'T0-HKJ-DFG', '-CMVP4_Y1000-MIX')]
[('5', 'T0-HKJ-DFG', '-CMVP5_Y1000-MIX')]
[('6', 'T0-HKJ-DFG', '-CMVP6_Y1000-MIX')]
[('7', 'T0-HKJ-DFG', '-CMVP7_Y1000-MIX')]
如果需要过滤原始列表:
names = list(filter(_f, names))