栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Windows上验证文件名

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Windows上验证文件名

鉴于先前引用的MSDN文档中指定的要求,以下正则表达式应该做得很好:

public static boolean isValidName(String text){    Pattern pattern = Pattern.compile(        "# Match a valid Windows filename (unspecified file system).          n" +        "^          # Anchor to start of string.        n" +        "(?!        # Assert filename is not: CON, PRN, n" +        "  (?:      # AUX, NUL, COM1, COM2, COM3, COM4, n" +        "    CON|PRN|AUX|NUL|  # COM5, COM6, COM7, COM8, COM9,     n" +        "    COM[1-9]|LPT[1-9] # LPT1, LPT2, LPT3, LPT4, LPT5,     n" +        "  )        # LPT6, LPT7, LPT8, and LPT9...     n" +        "  (?:\.[^.]*)?       # followed by optional extension    n" +        "  $        # and end of string      n" +        ")          # End negative lookahead assertion. n" +        "[^<>:"/\\|?*\x00-\x1F]*     # Zero or more valid filename chars.n" +        "[^<>:"/\\|?*\x00-\x1F\ .]  # Last char is not a space or dot.  n" +        "$          # Anchor to end of string. ",         Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);    Matcher matcher = pattern.matcher(text);    boolean isMatch = matcher.matches();    return isMatch;}

请注意,此正则表达式对文件名的长度没有施加任何限制,但是实际的文件名可能会限制为260或32767个字符,具体取决于平台。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514928.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号