asp.net - how to fetch the data from string input and save into datatable -
i want fetch data string input passenger name, base fare, total fare,commission, fop. field, showing in below image. field, want store in data table. column name is:- passenger name, base fare, total fare,commission, fop.
this string input:-
string request= @"tst00009 dca1s211e rh/07feb f n ld 13feb13 od sansan si t-e fxp/r,u 3.macdorman/sam corbin (chd) 1 san ba 272 n 24jun 825p ok nhxuqcnj ch 24jun24jun 1pc 2 o lhr ba 273 n 07aug 325p ok nhxuqcnj ch 07aug07aug 1pc san fare u usd 492.00 tx001 x usd 458.00yqac tx002 x usd 5.50ycae tx003 x usd 17.20usap tx004 x usd 17.20usas tx005 x usd 5.00xaco tx006 x usd 7.00xycr tx007 x usd 2.50ayse tx008 x usd 105.61gbad tx009 x usd 62.66ubas tx010 x usd 4.50xf total usd 1177.17 grand total usd 1177.17 san ba lon m/bt nhxuqcnj/ch ba san m/bt nhxuqcnj/ch end roe1.000000 xf san4.5 bt1195364*ba *fm0.00 40.fe ba only/nonref/nonend see ggairbagusa -bg:ba 45.fm *m*0.00a 46.fp check";
it seems you'd well-served learn more basic regular expressions.
regular-expressions.info 1 of favorite resources. said, helpful start.
it might cleaner grab each piece individually rather 1 single expression. this:
match m1 = regex.match(request, @"fxp/r,u\s+(.+?) \(chd\)", regexoptions.singleline); string name = m1.groups[1].value; match m2 = regex.match(request, @"fare u usd\s+(\d+\.\d{2})"); string fare = m2.groups[1].value; match m3 = regex.match(request, @"grand total usd\s+(\d+\.\d{2})"); string total = m3.groups[1].value; match m4 = regex.match(request, @"45\.fm (\s+)"); string commission = m4.groups[1].value; match m5 = regex.match(request, @"46\.fp (\s+)"); string fop = m5.groups[1].value;
if want, manage in one, it's kind of messy-looking:
string pattern = @"fxp/r,u\s+(?<name>.+?) \(chd\).*fare u usd\s+(?<fare>\d+\.\d{2}).*grand total usd\s+(?<total>\d+\.\d{2}).*45\.fm (?<commission>\s+).*46\.fp (?<fop>\s+)"; match m = regex.match(request, pattern, regexoptions.singleline); console.writeline("name: {0}", m.groups["name"]); console.writeline("fare: {0}", m.groups["fare"]); console.writeline("total: {0}", m.groups["total"]); console.writeline("commission: {0}", m.groups["commission"]); console.writeline("fop: {0}", m.groups["fop"]);
working example of this: http://ideone.com/0bhuw9
you may have adjust these patterns slightly. work given example, made assumptions how match (for example, assumes "commission" , "fop" don't contain spaces). hope helps.