连字符字符串上的正则表达式?c#



我试图匹配一个文件名参数与一个正则表达式验证。我在数字后面有连字符的问题…

字符串:article_rar_scout_13 - 03 - 14. - pptx

正则表达式:

private bool IsValidFileName(string FileName)
    {
        if (Regex.IsMatch(FileName, @"^Article[A-Z]{3}_Scout_[0-31]-[0-12]-[0-99].pptx$"))
        {
            return true;
        }
        else
        {
            throw new Exception("Please provide a correct file name (e.g. Drillinginfo_WAF_Scout_13-03-14.pptx");
        }                     
    }

在字符类0到9中[0-9]只可能,0到31 [0-31]将不可能。这是你的正则表达式的问题,而不是与连字符。你的正则表达式应该是

^Article_[A-Z]{3}_Scout_(?:0[1-9]|[1-2][0-9]|3[01])-(?:0[1-9]|1[012])-(?:[1-9][0-9]|0[1-9]).pptx
演示

最新更新