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

Ste*_*ven 5 c# regex

我需要验证用户是否以以下格式输入文本:

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

我可以使用Regex.Match吗?

Cha*_*ion 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);
}
Run Code Online (Sandbox Code Playgroud)