【发布时间】:2019-10-01 16:47:58
【问题描述】:
我创建了自己的正则表达式,除了反斜杠之外一切正常。我尝试了我的版本,但没有一个有帮助。
var regexItem = new Regex("[^A-Za-z0-9_.,&/*:;=+{}()'\"\\ -]+");
string temp2 = "";
while ((@line = file2.ReadLine()) != null)
{
if (regexItem.IsMatch(line) && (line.Contains(".jpg") || line.Contains(".png") || line.Contains(".jpeg") || line.Contains(".svg")))
{
@temp2 = Regex.Replace(line, "[^A-Za-z0-9_.,&/\\*:;=+{}()'\" -]+", "");
postki.WriteLine(@temp2);
Console.WriteLine(@"{0} ==> {1}", @line, @temp2);
}
else
{
postki.WriteLine(@line);
}
}
【问题讨论】:
-
反斜杠是 正则表达式和 C# 中的特殊字符。如果要在模式中使用文字反斜杠,则需要在正则表达式和 C# 中对其进行转义。
-
其实我看不懂。 :'D
-
有时拉出需要转义的字符更容易:
char doubleQuote = '"'; string s = "he said," + doubleQuote + "hello!" + doubleQuote;