python - 将 .htm 或 .html 扩展名与 python RE 匹配

标签 python html regex file expression

正如标题中提到的,我需要匹配 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/

相关文章:

python - 重新抛出 python 异常。抓哪个?

python - Django + Celery + SQS 设置。 Celery 通过 ampq 连接到默认的 RabbitMQ

html - float 子 div 仅填充剩余空间

java - 如何修复正则表达式以剥离 <blockquote></blockquote> 标签,其中包含\n ?

python - 包含空值列的 MySQL 查询

python - 同时向两个数据库插入数据

ios - 如何在 Cordova 中播放 HTML5 视频时忽略铃声设置

html - 使链接提交与默认按钮行为相同

ruby - 使用 ruby​​ 正则表达式在字符串中提取多次出现的模式

python - 删除特定属性小于或等于 0 的行