Reputation: 179
Below I have a sample of a log file that I am working with:
5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.
Almost every line is like this, but some will say Savings not found. Below is the most important part of this sample:
Savings found: $257.18
I am trying to write a piece of code that will look through this log file, search through the whole file, and if savings were found, it will record that number into a variable, that way when I total it up, it will just fall into a variable.
The problem I am having right now is getting my code to display just that number. Below is what I have been working on thus far:
foreach (string line in gdsGroup)
{
Match m = Regex.Match(line, @"Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
if (m.Success)
{
decimal gdsNumberSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
decimal gdsNumberPercent = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);
string prefix = string.Empty;
if (gdsNumberPercent >= 30)
{
if (gdsNumberSavings >= 500)
prefix = "**";
else
prefix = "*";
}
Console.WriteLine(prefix + line + "\n");
Console.WriteLine(gdsNumberSavings);
}
}
}
The problem that is happening is that after the line that gives me what I have above, it prints out the of the savings for that line. So my question is, should I continue to try to go about a variable way, or do I need a regular expression to isolate that value, and if I need a regular expression what would the expression be?
Upvotes: 0
Views: 60
Reputation: 9041
There's nothing wrong with the Regex you are using to obtain the value you are wanting from each line in your foreach
.
You just need an accumulator initialized before you start the foreach
and add gdsNumberSavings
to it whenever there is a match.
string[] lines =
{
"5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.",
"5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.",
"5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.",
"5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found."
};
// Initialize total
decimal total = 0;
foreach (string line in lines)
{
Match m = Regex.Match(line, @"Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
if (m.Success)
{
decimal gdsNumberSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
decimal gdsNumberPercent = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);
string prefix = string.Empty;
if (gdsNumberPercent >= 30)
{
prefix = gdsNumberSavings >= 500 ? "**" : "*";
}
Console.WriteLine(prefix + line + "\n");
Console.WriteLine(gdsNumberSavings);
// Accumulate into a total
total += gdsNumberSavings;
}
}
// Display total
Console.WriteLine("Total Savings: {0}", total);
Upvotes: 1
Reputation: 508
I'm not sure as for the variable side of the question. But as for the regular expression, this will find your dollar amounts: \$[0-9]+[.]\d{2}
You have three dollar amounts in that line, so just take the second instance that would be returned from the expression and that would be your total savings.
Upvotes: 0