当前位置:网站首页>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
边栏推荐
- mysql基本概念之索引
- 使用TD-workbench管理tDengine数据库数据
- 通过字符设备虚拟文件系统实现kernel和userspace数据交换(基于kernel 5.8测试通过)
- 梅科尔工作室-PR第三次培训笔记(效果与转场及插件使用)
- 跳转到微信小程序方法
- The working principle and industry application of AI intelligent image recognition
- LVS负载群集--DR模式
- 梅科尔工作室-Pr第二次培训笔记(基本剪辑操作和导出)
- XSS跨站脚本攻击详解以及复现gallerycms字符长度限制短域名绕过
- 四大组件之一BroadCast(其一)
猜你喜欢
随机推荐
mysql基本概念之索引
NodeRed系列—创建mqtt broker(mqtt服务器),并使用mqttx进行消息发送验证
华为手机软键盘挡住Toast
动画(其二)
梅科尔工作室-华为云ModelArts第一次培训
GBase 8a MPP Cluster产品支撑的平台
关于安全帽识别系统,你需要知道的选择要点
【OAuth2】授权机制
GBase 8s是如何保证数据一致性
跳转到微信小程序方法
windows下的redis安装及密码修改
动画(其一)
架构设计杂谈
mysql 间隙锁(GAP-LOCK)演示
【高德地图】易采坑合集
CVPR2022——Not All Points Are Equal : IA-SSD
梅科尔工作室-HarmonyOS应用开发的第二次培训
>>开发工具:开发工具排名对比
GBase 8a语法格式
第七届集美大学程序设计竞赛(个人赛)题解










