【发布时间】: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 的负面回溯,但我需要正则表达式来匹配整行。
【问题讨论】:
-
使用锚点,也许让匹配更具体
^http://.*\.(?:jpg|gif)$regex101.com/r/u2OHGC/1