1
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);

I am trying to use this pattern check to validate file names(only .txt text files), but this pattern doesn't allow file names which contain periods, eg: "a.b". Does any one suggest what i am missing?

MChaker
  • 2,610
  • 2
  • 22
  • 38
crazyforjava
  • 65
  • 2
  • 9

1 Answers1

0

I think the 4th last line commented with "# Zero or more valid filename chars." should be without dot (.):

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

(And I'm not sure what the quantifier + in the second to last line is here for).

See also Validate a file name on Windows

Community
  • 1
  • 1
isnot2bad
  • 24,105
  • 2
  • 29
  • 50