正如标题中提到的,我需要匹配 Python 中的 .htm 或 .html 文件扩展名。 我尝试使用标准库中的 RE 模块,但找不到正确的模式。 我测试了几种模式,(对我来说)看起来更正确但不起作用的模式如下:
re.search("\.(htm|html)",file)
re.search("\.htm(l)",file)
re.search("\.htm(l?)",file)
re.search("\.htm(l*?)",file)
re.search("\.htm(l+?)",file)
和其他变体,但都不起作用。 问题是,这些模式识别像 .html 或类似的文件扩展名,我不想要它们(只有 htm 和 html)。
有人可以帮我找到正确的模式吗? 谢谢大家
最佳答案
在这种情况下不需要正则表达式,请使用 endswith()相反,即:
if filePath.lower().endswith(('.html', '.htm')):
关于python - 将 .htm 或 .html 扩展名与 python RE 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029282/