图像文件扩展名正则表达式模式
([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
描述
( #Start of the group #1
[^\s]+ # must contains one or more anything (except white space)
( # start of the group #2
\. # follow by a dot "."
(?i) # ignore the case sensive checking for the following characters
( # start of the group #3
jpg # contains characters "jpg"
| # ..or
png # contains characters "png"
| # ..or
gif # contains characters "gif"
| # ..or
bmp # contains characters "bmp"
) # end of the group #3
) # end of the group #2
$ # end of the string
) #end of the group #1
整个组合是指,必须具有1个或多个字符串(但不能有空格),后跟点“。”。 字符串以“ jpg”或“ png”或“ gif”或“ bmp”结尾,而且expand的文件不区分大小写。
此正则表达式模式广泛用于不同文件的广泛检查。 您只需更改结束组合(jpg | png | gif | bmp)即可进行不同的文件扩展名检查,以适应您的需要。
Java正则表达式示例
package com.mkyong.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ImageValidator{
private Pattern pattern;
private Matcher matcher;
private static final String IMAGE_PATTERN =
"([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";