当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
NAT模式 LVS负载均衡群集部署
LNMP源码搭建
GBase 8s与Oracle存储对比
安全帽识别算法
RecycleView
安全帽识别系统
xss.haozi靶场通关
软件架构之--MVC、MVP、MVVM
GBase 8s中IO读写方法
CVPR2022——A VERSATILE MULTI-VIEW FRAMEWORK
梅科尔工作室-深度学习第二讲 BP神经网络
Redis哨兵模式
OSI TCP/IP学习笔记
LAMP架构介绍及配置
梅科尔工作室-第四次PR培训笔记(字幕和标题动画,关键帧动画和声音处理)
CVPR2020:Seeing Through Fog Without Seeing Fog
GBase 8a MPP Cluster产品高级特性
LiDAR Snowfall Simulation for Robust 3D Object Detection
跳转到微信小程序方法
GBase 8s与Oracle锁对比










