【问题标题】:Regex to match file extension in URL [duplicate]正则表达式匹配 URL 中的文件扩展名 [重复]
【发布时间】:2019-09-04 09:22:45
【问题描述】:

我有以下正则表达式regex101 with test cases

.{0,}(\.jpg|\.gif).{0,}

我想匹配 URL 末尾包含 .gif 和 .jpg 扩展名的 URL。不幸的是,该域被命名为 gif.com。

这是我的测试用例:

1 - http://www.gif.com/test.jpg - should match
2 - http://www.gif.com/test.html - shouldn't match
3 - http://gif.com/test.jpg - should match
4 - http://gif.com/test.html - shouldn't match

我想将正则表达式更改为在情况 2 中不匹配。我尝试了一些对 http://www 的负面回溯,但我需要正则表达式来匹配整行。

【问题讨论】:

标签: java regex


【解决方案1】:

需要使用$锚点,表示字符串结束

.{0,}(\.jpg|\.gif)$

Regex Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2013-10-16
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多