2010-11-03 57 views

回答

7

我会做这样的事情:

private static readonly Regex _validator = 
    new Regex(@"^\d{4}-\d{5}-\d{4}-\d{3}$", RegexOptions.Compiled); 
private static bool ValidateInput(string input) 
{ 
    input = (input ?? string.Empty); 
    if (input.Length != 19) 
    { 
     return false; 
    } 
    return _validator.IsMatch(input); 
} 
+0

,只有不适合的数字?诚然,这个问题并没有具体说明“文本”是什么。 – 2010-11-03 15:27:32

+0

@Liviu - 我通常将**#**解释为数字占位符。 – ChaosPandion 2010-11-03 15:28:42

+0

为什么要经过检查长度和多次返回的麻烦,是预编译的正则表达式操作如此昂贵以至于不值得这样做?我会让它失败的正则表达式,并使该方法更简单。不过我喜欢你使用\ d而不是[0-9] – dstarh 2010-11-03 15:34:46