Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式与URL中的文件扩展名匹配_Java_Regex - Fatal编程技术网

Java 正则表达式与URL中的文件扩展名匹配

Java 正则表达式与URL中的文件扩展名匹配,java,regex,Java,Regex,我有下面的正则表达式 我想在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

我有下面的正则表达式

我想在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中不匹配。我已经尝试了一些反向查找,但是我需要正则表达式来匹配整行。

您需要使用
$
锚点,这意味着字符串结束

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

使用锚定,可能会使匹配更加具体一些
^http://.\(?:jpg|gif)$
.{0,}(\.jpg|\.gif)$