C# Regex Issue “unrecognized escape sequence”

Use @ to make the strings no longer use the escape character \:

string regexPattern1 = @"^(\d{3}\.){2}\d{4}$";
string regexPattern2 = @"^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$";

As a side note, I think you want the two ifs at the end to be a single if with an or (||) between the two conditions.

Leave a Comment