当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
快照读下mvcc实现避免幻读
RecycleView
azkaban集群部署
【uniapp】跨端开发问题记录
SCNet:Semantic Consistency Networks for 3D Object Detection
基于ijkplayer 0.8.8编译的完整so. libijkffmpeg.so等,支持ssl h265, rm, rmvb
若依分离版—增加通知公告预览功能
基于uniapp开发的聊天界面
架构设计杂谈
centos—docker安装mysql
GBase数据库监控
DOM破坏
GBase 8s性能简介
使用TD-workbench管理tDengine数据库数据
华为手机软键盘挡住Toast
解决Glide图片缓存问题,同一url换图片不起作用问题
mysql基本概念之索引
GBase 8s与Oracle锁对比
NodeRed系列—nodered安装及基本操作
跳转到微信小程序方法