当前位置:网站首页>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 .
- 文件名不能为空,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.的文件
- 文件名中不能包含\/:*?”<>|中的任意字符
- 文件名(包括扩展名)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:
- There cannot be blank characters at the beginning and end(空格、制表符、换页符等空白字符的其中任意一个),The filename cannot end with .号
- The filename and extension cannot be both empty
- 文件名中不能包含\/:*?”<>|中的任意字符
- 文件名(包括扩展名)must not be longer than 255个字符
- 在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
边栏推荐
猜你喜欢
随机推荐
四大组件之一BroadCast(其一)
Nodered系列—写入tDengine超级表,自动创建子表
@2022-02-22:每日一语
centos—docker安装mysql
LAMP架构介绍及配置
mysql基本概念之存储引擎
跳转到微信小程序方法
【mysql】查询不区分大小写(用户密码登录不区分大小写)
DNS外带注入SQLMAP
浙江大学软件学院2020年保研上机真题练习
Reconstruction and Synthesis of Lidar Point Clouds of Spray
通用的 kernel和 userspace Makefile
对MySQL查询语句的分析
Redis分布式锁
安全帽识别系统
动画(其二)
Fragment 和 CardView
安全帽识别系统-解决监管难题
秦始皇到底叫嬴政还是赵政?
azkaban集群部署










