Reputation: 2634
I need to verify that the text of a file contains text in the following format:
#0
DIRECTION: FORWARD
SPEED: 10
TIME: 10
OR
#1
DIRECTION: REVERSE
SPEED: 10
ROTATIONS: 10
And this will repeat with multiple steps.
Where #
must be followed by a number, DIRECTION
must be followed by FORWARD
or REVERSE
, SPEED
must be followed by a number and the last line will be either TIME
or ROTATIONS
followed by a number.
I want to verify that the file contains this text in this format and not some weird values before I start reading in the values.
Can I use some type of wild card? I have started looking at Regex, but I have not worked with it before.
What I would like to do is do some type of comparison where the number are wildcards, that way I know if the lines contain the basic formatting:
if(fileLines[0].Matches("#%")) // Where % would be the wildcard?
if(fileLines[1].Matches("DIRECTION:%")) // Does something like this exist?
Upvotes: 1
Views: 728
Reputation: 216353
This patterns seems to work
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);
}
Upvotes: 1
Reputation: 4361
Provided you have read all the lines in the file as a generic list.
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");
Make use of this function to verify if the file is valid
Credit : Steve for the 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);
}
Call the function to check the consistancy
bool status = CheckConsistancy(fileLines);
Upvotes: 0