无法识别的字符 \C 无法解析。为什么?

Unrecognized character \C is irresolvable. Why?

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)");

两种方式都给我错误“无法识别的转义字符 \C。我很困惑为什么。

我需要更改什么才能获得此结果?

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

完整错误:未处理的异常:系统。 ArgumentException:正在解析 "\server1\Cold Folder1\(title and text number.*\.pdf)"

在问题正则表达式的第一行:

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

错误在 \Cold\(title - 它应该有双斜杠 \\Cold\\(title

在问题正则表达式的第二行:

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

有两个错误:

  • \Cold 应该有双斜杠来表示斜杠:\Cold
  • \(title的意思是(,应该是\(title来表示\StartGroup

工作代码:

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

你试过在两者中都使用@吗?输入和正则表达式匹配

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

已编辑:谢谢 Alan Moore

这应该有效

希望对您有所帮助