2

Below I have a sample of a line from a log file that I have been working with for a while now:

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.

In this file I have categories that I need to capture: First is the date and time, the next is the Locator which is the NXRMN5, Savings found and the percent (if any) which in this line is $257.18 11.5%.

In the Console I would want it to look like this:

Date:  5/21/2015 11:55:56 PM 
Locator: NXRMN5
Dollar Savings: $257.18 
Percent Savings: 11.55 %

Below I have some code that I have been working on for quite some time:

foreach (string line in gdsLines)
        {
            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);

                Console.WriteLine("Dollar Savings: $"  + gdsNumberSavings + " Percent Savings: " + gdsNumberPercent + " % " + "\n");
                gdsTotalSavings += gdsNumberSavings;
                gdsTotalPercentage += gdsNumberPercent;
            }
        } 

This code that I provided takes the regex I made and then puts both the savings found and the percent and then prints the values in the console. Don't worry about the gdsTotal variables that for something else. The main concern is in the regex. In order to add in the other elements that I need to, do I need to create another regex, or can I just add on to it? Either way, could someone please help me in the forming of this regex? Any help would be very appreciative!

1

1 Answer 1

4

Let me suggest this solution:

  1. Date is extracted by mere splitting the string with | and getting the first element
  2. The Locator is captured with regex.

The code is as follows:

string line = "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.";
string date = line.Split('|').Take(1).FirstOrDefault().Trim();
Match m = Regex.Match(line, @"Processed\s+(?<locator>[^.]+)\.{3}.*?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 locator = m.Groups["locator"].Value;

     Console.WriteLine("Date:  " + date + ", Locator: " + locator + "Dollar Savings: $" + gdsNumberSavings + " Percent Savings: " + gdsNumberPercent + " % " + "\n");
     gdsTotalSavings += gdsNumberSavings;
     gdsTotalPercentage += gdsNumberPercent;
}

Output:

Date:  5/21/2015 11:55:56 PM, Locator: NXRMN5Dollar Savings: $257,18 Percent Savings: 11,55 % 

Or, you can use the same logic in a single regex:

Match m = Regex.Match(line, @"^(?<date>[^|]+)\|.*Processed\s+(?<locator>[^.]+)\.{3}.*?Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");

And inside the if block:

string date = m.Groups["date"].Value.Trim();
Sign up to request clarification or add additional context in comments.

4 Comments

Nice consistent concatenation for your output there... nawt. Still gets important stuff across so worth an upvote
This works, but for everyone who looks at this in the future, make sure you do add the locator statement in the if block as well as that date statement.
Do you mean the regex-only way? Yes, I mention that. Actually, I would not create temporary variables, just use string.Format() and create the string to display.
I like the way you used groups to grab all the data at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.