Validate a file name on Windows

后端 未结 11 1977
一生所求
一生所求 2020-11-29 23:28
public static boolean isValidName(String text)
{
    Pattern pattern = Pattern.compile(\"^[^/./\\\\:*?\\\"<>|]+$\");
    Matcher matcher = pattern.matcher(text         


        
11条回答
  •  遥遥无期
    2020-11-30 00:22

    Looks good. At least if we believe to this resource: http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx

    But I'd simplify use the code. It is enough to look for one of these characters to say that the name is invalid, so:

    public static boolean isValidName(String text)
    {
        Pattern pattern = Pattern.compile("[^/./\\:*?\"<>|]");
        return !pattern.matcher(text).find();
    }
    

    This regex is simpler and will work faster.

提交回复
热议问题