C# Regex Issue “unrecognized escape sequence”

I have an issue with the regular expressions I'm using but don't know how to continue with them. I get the error "unrecognized escape sequence".

I am trying to list out all files that could have a phone number in the formats listed in the code below

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

Any help is much appreciated.

24
задан kennytm 27 May 2011 в 16:36
поделиться