编辑:因为你们让我很头疼,而且没有真正说太多,所以我只是拼凑了这次尝试,因为有人想要它。它完全符合我的要求,是的,事实证明 Regex 正是我需要做的事情。
int counter = 0;
string line;
string temp;
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\final.header");
while ((line = file.ReadLine()) != null)
{
/*if (counter == 5 && Regex.IsMatch(line, @"Read\=.*"))
{
counter = counter - 2;
}*/
switch (counter)
{
case 0:
temp = Regex.Replace(line, "##File List", string.Empty);
//Console.WriteLine(temp);
break;
case 1:
Console.WriteLine(line);
break;
case 2:
Console.WriteLine(line);
break;
case 3:
Console.WriteLine(line);
break;
case 4:
temp = line.Replace("Cc=", "");
//Console.WriteLine(temp);
break;
case 5:
if (Regex.IsMatch(line, @"Read\=.*"))
{
Console.WriteLine(line);
counter +=1;
}
else
{
temp = Regex.Replace(line, @"AttachmentName\=.*", string.Empty);
}
break;
case 6:
Console.WriteLine(line);
break;
case 7:
temp = Regex.Replace(line, @"parentId\=.*", "");
//Console.WriteLine(temp);
break;
case 8:
temp = Regex.Replace(line, @"Bcc\=.*", "");
//Console.WriteLine(temp);
break;
case 9:
temp = Regex.Replace(line, @"Date\=.*", "");
//Console.WriteLine(temp);
break;
case 10:
temp = Regex.Replace(line, @"format\=.*", "");
//Console.WriteLine(temp);
break;
case 11:
if (Regex.IsMatch(line, @"To\=.*"))
{
Console.WriteLine(line);
counter += 1;
}
else
{
temp = Regex.Replace(line, @"AttachmentId\=.*", "");
}
//Console.WriteLine(temp);
break;
case 12:
Console.WriteLine(line);
break;
}
if (counter >= 12)
{
counter = 0;
}
else
{
counter++;
}
}
我试图弄清楚如何在 C# 中分隔/拆分它,甚至不知道从哪里开始。鉴于以下文本是从 StreamReader 读入的,我发现的所有关于使用 string.split 和 regex.split 的示例都让我感到困惑。
System.IO.StreamReader myFile = new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();
测试.txt:
##File List
#Tue Dec 13 14:27:43 CST 2011
Subject=Research paper.
From=zmeinecke
Cc=
AttachmentName=ADHD Medication Research Paper.docx
Read=true
parentId=
Bcc=
Date=1323748746221
format=blackboard.base.FormattedText$Type\:HTML
AttachmentId=b2cb1016f0b847a3bfae636988aa3f6a
To=ksanger;
基本上,我试图让它成为输出:
Tue Dec 13 14:27:43 CST 2011
Subject=Research paper.
From=zmeinecke
Read=true
To=ksanger;
谢谢