当前位置:网站首页>Use regex to verify whether the file name is legal

Use regex to verify whether the file name is legal

2022-08-11 06:16:00 , as you like

 我以前都是用 File 类的 createNewFile() 方法.当然,This method really works.但当要批量验证时,You can't create files one by one.

So I thought of the regular, The overhead of regular matching is not known how many times smaller than creating a file.
Google了一下WinThe platform's filename rules,并实践了一下.


Well a legitimate document(Win下)The following rules should be followed .

  1. 文件名不能为空,Empty has two meanings here
    • 文件名(包括扩展名)长度为0or consist of only null characters(包括\t\band other invisible escape characters)
    • The filename and extension cannot be both empty.But we can actually create something similar programmatically.project,..txt等形式的文件,But can't create something similarabc.的文件
  2. 文件名中不能包含\/:*?”<>|中的任意字符
  3. 文件名(包括扩展名)must not be longer than 255个字符

In fact like”..”(不包含引号,下同)file also cannot be created.
There are similar illegal documents” aa”, “aa “, “aa.”(will be created as”aa”,Also count it as illegal),”a\ta”(\tInvisible characters such as tabs(除空格外))

So we get a more detailed specification of the file name naming rules:

  1. There cannot be blank characters at the beginning and end(空格、制表符、换页符等空白字符的其中任意一个),The filename cannot end with .号
  2. The filename and extension cannot be both empty
  3. 文件名中不能包含\/:*?”<>|中的任意字符
  4. 文件名(包括扩展名)must not be longer than 255个字符
  5. 在1.的条件下,Any null characters other than spaces cannot appear in the file name.The presence of control characters is actually illegal,But because the situation is too complicated,Don't judge.

So there is the following match

首字符: [^\s\\/:\*\?\"<>\|]
尾字符: [^\s\\/:\*\?\"<>\|\.]
其它字符: (\x20|[^\s\\/:\*\?\"<>\|])*

\s Only the following six characters can be matched(via: java.util.regex.Pattern):

半角空格( )
水平制表符(\t)
竖直制表符
回车(\r)
换行(\n)
换页符(\f)

用Java语言实现:

public static boolean isValidFileName(String fileName) {
    
    if (fileName == null || fileName.length() > 255) 
        return false; 
    else 
        return fileName.matches( 
           "[^\\s\\\\/:\\*\\?\\\"<>\\|](\\x20|[^\\s\\\\/:\\*\\?\\\"<>\\|])*[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]$"); 
}

用于测试:

System.out.println("null(未初始化)" + "\t" + isValidFileName(null)); 
System.out.println(" .xml" + "\t" + isValidFileName(" .xml")); 
System.out.println(".xml " + "\t" + isValidFileName(".xml ")); 
System.out.println(" .xml " + "\t" + isValidFileName(" .xml ")); 
System.out.println(".xml." + "\t" + isValidFileName(".xml.")); 
System.out.println(".xml" + "\t" + isValidFileName(".xml")); 
System.out.println("    .xml(制表符)" + "\t" + isValidFileName("    .xml")); 
System.out.println(".." + "\t" + isValidFileName("..")); 
System.out.println("fdsa    fdsa(制表符)" + "\t" + isValidFileName("fdsa    fdsa(制表符)")); 
System.out.println("a.txt" + "\t" + isValidFileName("a.txt"));

结果:

null(未初始化)      false 
 .xml   false 
.xml    false 
 .xml   false 
.xml.   false 
.xml    true 
    .xml(制表符)   false 
..      false 
fdsa    fdsa(制表符)       true 
a.txt   true

原网站

版权声明
本文为[, as you like]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110514299764.html