简体   繁体   English

C#正则表达式帮助 - 验证输入

[英]C# regex help - validating input

I need to validate that the user entered text in the format: 我需要验证用户是否以以下格式输入文本:

####-#####-####-###

Can I do this using Regex.Match? 我可以使用Regex.Match吗?

I would do something like this: 我会做这样的事情:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM