I need to validate that the user entered text in the format:
####-#####-####-###
Can I do this using 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);
}
- characters, code point 45 HYPHEN-MINUS, be better written as \p{Pd} for any sort of dash punctuation? And if you wanted them to be all of the same sort, you could write ^\d{4}(\p{Pd})\d{5}\1\d{4}\p{Pd}\1\d{3}$.