C#正则表达式发出“无法识别的转义序列”

| 我正在使用的正则表达式有问题,但不知道如何继续使用它们。我收到错误“无法识别的转义序列”。 我正在尝试列出下面代码中列出的格式的所有可能具有电话号码的文件
static void Main(string[] args)

    {
        //string pattern1 = \"xxx-xxx-xxxx\";
        //string pattern2 = \"xxx.xxx.xxxx\";
        //string pattern3 = \"(xxx) xxx-xxxx\";

        string[] fileEntries = Directory.GetFiles(@\"C:\\BTISTestDir\");

        foreach (string filename in fileEntries)
        {
            StreamReader reader = new StreamReader(filename);
            string content = reader.ReadToEnd();
            reader.Close();

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

            if(Regex.IsMatch(content, regexPattern1))
                Console.WriteLine(\"File found: \" + filename);
            if(Regex.IsMatch(content, regexPattern2))
                Console.WriteLine(\"File found: \" + filename);
        }

        Console.WriteLine(Environment.NewLine + \"Finished\");
        Console.ReadLine();
    }
任何帮助深表感谢。     
已邀请:
        使用
@
使字符串不再使用转义字符
\\
string regexPattern1 = @\"^(\\d{3}\\.){2}\\d{4}$\";
string regexPattern2 = @\"^((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}$\";
顺便说一句,我认为您希望最后的两个
if
是一个单独的ѭ4the,且两个条件之间有或(
||
)。     
        添加其他\'\\\'以取消转义。处理后,将按照您期望的方式进行解释。     
        问题不在于正则表达式,而是字符串。在通过调用IsMatch()将其编译为正则表达式之前,您输入的文本仍然是普通字符串,并且必须遵守语言规则。 您语言中的\\ d无法识别转义序列,因此会出现错误。您可以使用双反斜杠(\\是转义序列来获取),也可以如Blindy所指出的那样,可以在常量字符串前加上@作为前缀,告诉编译器不应尝试将任何看起来像转义序列的内容解释为它。     

要回复问题请先登录注册