Python 如果大海捞针

Python 如果大海捞针,python,Python,我知道我能做到: if 'hello' in 'hello world': 如果我有几个指针,比如('.css'、'.js'、'.jpg'、'.gif'、'.png'、'.com'),我想检查它们是否在一个字符串中 (注意:endswith在这种情况下不起作用,它们可能不是后缀)您可能会发现有用的: 您可以使用正则表达式进行“多重匹配”: 输出 file.css file.js 请注意,此解决方案使您能够在任意数量的文件名上运行,并将其与多个不同的扩展名进行比较 haystack = 'he

我知道我能做到:

if 'hello' in 'hello world':
如果我有几个指针,比如('.css'、'.js'、'.jpg'、'.gif'、'.png'、'.com'),我想检查它们是否在一个字符串中

(注意:
endswith
在这种情况下不起作用,它们可能不是后缀)

您可能会发现有用的:

您可以使用正则表达式进行“多重匹配”:

输出

file.css
file.js
请注意,此解决方案使您能够在任意数量的文件名上运行,并将其与多个不同的扩展名进行比较

haystack = 'hello world'
needles = ['.css', '.js', '.jpg', '.gif', '.png', '.com']
if any(needle in haystack for needle in needles):
    pass  # ...
import re
pat = r'(\.css|\.js|\.jpg|\.gif|\.png|\.com)'
files = ['file.css', 'file.exe', 'file.js', 'file.bat']
for f in files:
    if re.findall(pat, f):
        print f
file.css
file.js