我需要验证文件的文本是否包含以下格式的文本:
#0
DIRECTION: FORWARD
SPEED: 10
TIME: 10
或
#1
DIRECTION: REVERSE
SPEED: 10
ROTATIONS: 10
这将通过多个步骤重复进行。
其中,#后面必须跟一个数字,DIRECTION后面必须跟FORWARD或REVERSE,SPEED后面必须跟一个数字,最后一行必须是TIME或ROTATIONS,后面跟一个数字。
在开始读取值之前,我想验证文件中是否包含这种格式的文本,而不是一些奇怪的值。
我可以使用某种类型的通配符吗?我已经开始考虑Regex了,但我以前没有使用过它。
我想做的是在数字是通配符的情况下进行某种类型的比较,这样我就可以知道行是否包含基本格式:
if(fileLines[0].Matches("#%")) // Where % would be the wildcard?
if(fileLines[1].Matches("DIRECTION:%")) // Does something like this exist?发布于 2012-05-25 23:49:04
这种模式似乎很有效。
string[] lines =
{
"#0",
"DIRECTION: FORWARD",
"SPEED: 10",
"TIME: 10"
};
// meaning At start of line there is a # followed by digits till the end of line
if(checkLine(@"^#\d+$", lines[0]) == false)
Console.WriteLine("False on line 1");
else
Console.WriteLine("True on line 1");
// meaning At start of line there is the word DIRECTION: followed by space and the words REVERSE or FORWARD
if(checkLine(@"^DIRECTION: (REVERSE|FORWARD)", lines[1]) == false)
Console.WriteLine("False on line 2");
else
Console.WriteLine("True on line 2");
// meaning At start of line there is the word SPEED: followed by a space and digits till the end of the line
if(checkLine(@"^SPEED: \d+$", lines[2]) == false)
Console.WriteLine("False on line 3");
else
Console.WriteLine("True on line 3");
// meaning At start of line there are the words TIME or ROTATIONS followed by colon, space and digits till the end of the line
if(checkLine(@"^(TIME|ROTATIONS): \d+$", lines[3]) == false)
Console.WriteLine("False on line 4");
else
Console.WriteLine("True on line 4");
}
// Define other methods and classes here
private bool checkLine(string regExp, string line)
{
Regex r = new Regex(regExp);
return r.IsMatch(line);
}发布于 2012-05-26 00:11:50
假设您已将文件中的所有行作为通用列表读取。
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的Regex implementation
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);https://stackoverflow.com/questions/10757003
复制相似问题