Using C# regex to get multiple data out of a string -


below have sample of line log file have been working while now:

5/21/2015 11:55:56 pm | batch 6|386/767|50.33 %|ch2m-r|processed nxrmn5...checking refundable , non-refundable fares. traditional booking. inside ticketing window. minimum savings required: $131.00. actual savings: $257.18. savings found: $257.18 (11.55 %). savings found. 

in file have categories need capture: first date , time, next locator nxrmn5, savings found , percent (if any) in line $257.18 11.5%.

in console want this:

date:  5/21/2015 11:55:56 pm  locator: nxrmn5 dollar savings: $257.18  percent savings: 11.55 % 

below have code have been working on quite 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 provided takes regex made , puts both savings found , percent , prints values in console. don't worry gdstotal variables else. main concern in regex. in order add in other elements need to, need create regex, or can add on it? either way, please me in forming of regex? appreciative!

let me suggest solution:

  1. date extracted mere splitting string | , getting first element
  2. the locator captured regex.

the code follows:

string line = "5/21/2015 11:55:56 pm | batch 6|386/767|50.33 %|ch2m-r|processed nxrmn5...checking refundable , non-refundable fares. traditional booking. inside ticketing window. minimum savings required: $131.00. actual savings: $257.18. savings found: $257.18 (11.55 %). savings 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, can use same logic in 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 if block:

string date = m.groups["date"].value.trim(); 

Comments