gpt4 book ai didi

Java 正则表达式与文件名匹配模式

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:45 26 4
gpt4 key购买 nike

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"
+ " +([.]txt) # followed by .txt \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"
+ " +([.]txt) # followed by .txt \n"
+ "$ # Anchor to end of string. ",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE| Pattern.COMMENTS);

我尝试使用此模式检查来验证文件名(仅限 .txt 文本文件),但此模式不允许文件名包含句点,例如:"a.b"。有人建议我缺少什么吗?

最佳答案

我认为最后第四行注释为“#零个或多个有效文件名字符。”应该不带点():

+ "[^<>:\"/\\\\|?*\\x00-\\x1F]*  # Zero or more valid filename chars.\n"

(我不确定倒数第二行中的量词 + 的用途)。

另请参阅Validate a file name on Windows

关于Java 正则表达式与文件名匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30039372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com