我有这样的文件列表:如何过滤带有特定前缀和后缀(扩展名)的文件名?
file_list = ['file1.zip', 'file1.txt']
file_prefix = 'file1'
我想使用filter
和re
只得到file1.txt
以上。我试试这个:
regex = re.compile(file_prefix + '.*(!zip).*')
result = list(filter(regex.search, file_list))
# in the above, result should be populated with just ['file1.txt']
但是,正则表达式模式不起作用。有人可以帮我解决这个问题吗?非常感谢先进!
用途: '(?!(\拉链*'re.compile file_prefix +)')' – anubhava
有什么特别的理由使用正则表达式这样简单的模式匹配?这有点过头了...... – zwer
@zwer你会推荐使用什么,而不是正则表达式? – user1330974