32

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Blake Rogers
  • 637
  • 2
  • 7
  • 13
  • 1
    Save yourself some trouble and use `(?:\(\d{3}\)|\d{3})[\s\.-]\d{3}[\.-]\d{4}` – Brad Christie May 27 '11 at 16:42
  • possible duplicate of [Unrecognized escape sequence for path string containing backslashes](http://stackoverflow.com/questions/1302864/unrecognized-escape-sequence-for-path-string-containing-backslashes) – JasonMArcher Feb 05 '15 at 00:06

3 Answers3

89

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.

Mamun
  • 66,969
  • 9
  • 47
  • 59
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • I could be wrong, and this may be a new format, but a [`string literal`](http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx) needs the `@` before the quote, not the variable name. – Brad Christie May 27 '11 at 16:38
  • @Blindy: You can be lucky there is no changelog for the answer yet ;p – leppie May 27 '11 at 16:39
  • Well, for what it's worth, `@identifier` *is* valid C# syntax <. – Blindy May 27 '11 at 16:43
  • @Blindly Thanks. Also for the thought of combining the if statements. Now it accepts my regex but i'm not getting any files returned and I know that it should be returning stuff... weird. I made a text file with those patterns in it. – Blake Rogers May 27 '11 at 16:58
  • @Blake, give me an example of the file names you made. They should be like `123.456.1234` with nothing else around them. – Blindy May 27 '11 at 18:31
  • [code] string regPattern1 = @"(\()(\d)(\d)(\d)(\))(\s+)(\d)(\d)(\d)(-)(\d)(\d)(\d)(\d)"; string regPattern2 = @"(\d)(\d)(\d)(\.)(\d)(\d)(\d)(\.)(\d)(\d)(\d)(\d)"; string regPattern3 = @"(\d)(\d)(\d)(-)(\d)(\d)(\d)(-)(\d)(\d)(\d)(\d)"; [/code] – Blake Rogers May 27 '11 at 19:05
9

Add an additional '\' to unescape the escape. When it's processed it will then be interpreted in the manner you intended.

  • 1
    +1 because this works beautifully in the rare case that you have to escape a double quote (") character. (ex. \\\") – AbeyMarquez May 10 '17 at 22:06
5

The problem is not the regex, but the string. Before compiling it to a regex with the call to IsMatch(), the text you enter is still a normal string and it must obey the language rules.

\d in your language is not a recognized escape sequence, hence the error. You can either double backslashes (\ is the escape sequence to get a ) or, as Blindy pointed out, you can prefix your constant strings with a @, telling the compiler that it should not try to interpret anything looking like an escape sequence to it.

King_DuckZ
  • 238
  • 1
  • 6