0
Match m = Regex.Match("\\\\server1\\Cold Folder1\\title and text number06220-03-15-2015.pdf", "\\\\server1\\Cold Folder1\\(title and text number.*\\.pdf)");


Match m = Regex.Match("\\\\server1\\Cold Folder1\\title and text number06220-03-15-2015.pdf", @"\\server1\Cold Folder1\(title and text number.*\.pdf)");

Both ways give me the error "Unrecognized Escape Character \C. And I am stumped as to why.

What do I have to change to get this result?

Console.WriteLine("{0} produces the filename: {1}.", m.Groups[0].Value, m.Groups[1].Value);

// \\server1\Cold Folder1\title and text number06220-03-15-2015.pdf produces the filename: title and text number06220-03-15-2015.pdf

Full error: Unhandled Exception: System. ArgumentException: parsing "\\server1\Cold Folder1\(title and text number.*\.pdf)"

dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
  • 1
    Put a `@` before the `"`, like `Match(@"` – xanatos Mar 23 '15 at 21:53
  • 7
    Hint: backslash is an escape character in regular expressions, *as well* as in C#. So if you want an actual backslash in the text to match, you need to double it in the regex... which means either `@"\\"` or `"\\\\"` – Jon Skeet Mar 23 '15 at 21:53
  • What? Here. I'll put up the complete error in the question. I've done everything you've already said. – dotnetN00b Mar 23 '15 at 22:02
  • @dotnetN00b: install a regex design program, like Expresso from Ultra Pico, and type your regex pattern into it. Expresso will show you a "visual" representation of your pattern which will help you understand what Jon Skeet told you. – Sam Axe Mar 23 '15 at 22:10
  • possible duplicate of [Unrecognized escape sequence for path string containing backslashes](http://stackoverflow.com/questions/1302864/unrecognized-escape-sequence-for-path-string-containing-backslashes) – Wiktor Stribiżew Mar 23 '15 at 22:50
  • @JonSkeet - I understanding what you were saying now. Thanks. – dotnetN00b Mar 24 '15 at 18:46

2 Answers2

1

Have you tried using @ in both? input and regex match

Match m = Regex.Match(@"\\server1\Cold Folder1\title and text number06220-03-15-2015.pdf", @"\\\\server1\\Cold Folder1\\(title and text number.*\.pdf)");

EDITED: Thanks Alan Moore

This should work

I hope this helps

Oscar Bralo
  • 1,912
  • 13
  • 12
  • 1
    He had the right number of backslashes already. All he needs to is switch to a verbatim string by adding the `@` as you said.. – Alan Moore Mar 23 '15 at 22:29
-1

In the first line in question regex:

"\\\\server1\\Cold Folder1\\(title and text number.*\\.pdf)"

error is in \\Cold and \\(title - it should have double slash \\\\Cold and \\\\(title

In the second line in question regex:

 @"\\server1\Cold Folder1\(title and text number.*\.pdf)"   

there are two errors:

  • \Cold should have double slash to represent slash: \\Cold
  • \(title mean (, should be \\(title to represent \ and StartGroup

Working code:

Match m = Regex.Match("\\\\server1\\Cold Folder1\\title and text number06220-03-15-2015.pdf", @"\\\\server1\\Cold Folder1\\(title and text number.*\.pdf)");
Console.WriteLine("{0} produces the filename: {1}.", m.Groups[0].Value, m.Groups[1].Value);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
cypizek
  • 327
  • 2
  • 6
  • 3
    Please explain what you have changed and why. Like it is now, it's not a good answer. I need to pick a hex editor and compare your code to OP's code to find the difference. – Thomas Weller Mar 23 '15 at 22:17