前提是您已将文件中的所有行作为通用列表阅读。
List<string> fileLines = new List<string>();
fileLines.Add("#0");
fileLines.Add("DIRECTION: FORWARD");
fileLines.Add("SPEED: 10");
fileLines.Add("TIME: 10");
fileLines.Add("#1");
fileLines.Add("DIRECTION: REVERSE");
fileLines.Add("SPEED: 10");
fileLines.Add("ROTATIONS: 10");
使用此功能验证文件是否有效
信用:Steve 用于正则表达式的实施
public bool CheckConsistancy(List<string> fileLines)
{
bool status = false;
if (fileLines != null && fileLines.Count > 0)
{
if(fileLines.Count % 4 == 0)
{
List<List<string>> fileLineGroups = fileLines.Select((x, i) => new { Index = i, Value = x }).GroupBy(x => x.Index / 4).Select(x => x.Select(v => v.Value).ToList()).ToList();
foreach (List<string> fileLineGroup in fileLineGroups)
{
if (checkLine(@"^#\d", fileLineGroup[0]))
{
if (checkLine(@"^DIRECTION: (REVERSE|FORWARD)", fileLineGroup[1]))
{
if (checkLine(@"^SPEED: \d", fileLineGroup[2]))
{
if (checkLine(@"^(TIME|ROTATIONS): \d", fileLineGroup[3]))
{
status = true;
}
else
{
status = false;
break;
}
}
else
{
status = false;
break;
}
}
else
{
status = false;
break;
}
}
else
{
status = false;
break;
}
}
}
else
{
status = false;
}
}
return status;
}
private bool checkLine(string regExp, string line)
{
Regex r = new Regex(regExp);
return r.IsMatch(line);
}
调用函数检查一致性
bool status = CheckConsistancy(fileLines);