我需要验证用户输入的文本格式:
####-#####-####-###
我可以使用 Regex.Match 执行此操作吗?
最佳答案
我会做这样的事情:
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);
}
关于C# 正则表达式帮助 - 验证输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4088619/