正弦曲线上的正则表达式? C#

时间:2014-07-04 11:49:15

标签: c# regex

我试图将文件名参数与正则表达式进行匹配以进行验证。我在数字后遇到连字符问题......

string: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");
        }                     
    }

1 个答案:

答案 0 :(得分:1)

仅在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

DEMO