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
Java regex for image filename?
提问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 Maciej Ziarko
回答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)$