java 图像文件名的Java正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7102890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:38:07  来源:igfitidea点击:

Java regex for image filename?

javaregex

提问by newbie

I have to filter filenames to match only image (png,jpg,gif,bmp) files, is possible to make a such regex or do I have to have different regex for every file type? It also cannot contain path like ../../ or /var/app/filename.exe etc.

我必须过滤文件名以仅匹配图像(png、jpg、gif、bmp)文件,是否可以制作这样的正则表达式,或者我是否必须为每种文件类型使用不同的正则表达式?它也不能包含像 ../../ 或 /var/app/filename.exe 等路径。

回答by fgysin reinstate Monica

I'll use something like this... This should cover most cases without going too much into crazy details...

我会使用这样的东西......这应该涵盖大多数情况,而不会过多地涉及疯狂的细节......

(.*/)*.+\.(png|jpg|gif|bmp|jpeg|PNG|JPG|GIF|BMP|JPEG)$

As a Java String this regex looks like this:

作为 Java 字符串,此正则表达式如下所示:

"(.*/)*.+\.(png|jpg|gif|bmp|jpeg|PNG|JPG|GIF|BMP|JPEG)$"

Hope that helps... :)

希望有帮助... :)

回答by user unknown

Pretty easy:

相当容易:

re = ".*\.(png|jpg|gif|bmp)"

as long as you don't need UPPERCASE and jpeg. :)

只要你不需要大写和 jpeg。:)

回答by nAkhmedov

This pattern is working for me: (.*/)*.+\.(png|jpg|gif|bmp|jpeg|PNG|JPG|GIF|BMP)$

这种模式对我有用: (.*/)*.+\.(png|jpg|gif|bmp|jpeg|PNG|JPG|GIF|BMP)$