regex - Regular expression replace in C# -
i'm new using regular expressions, and, based on few tutorials i've read, i'm unable step in regex.replace formatted properly.
here's scenario i'm working on... when pull data listbox, want format csv format, , save file. using replace option ideal solution scenario?
before regular expression formatting example.
firstname lastname salary position ------------------------------------- john smith $100,000.00 m
proposed format after regular expression replace
john smith,100000,m
current formatting status output:
john,smith,100000,m
*note - there way can replace first comma whitespace?
snippet of code
using(var fs = new filestream(filepath, filemode.openorcreate, fileaccess.write)) { using(var sw = new streamwriter(fs)) { foreach (string stw in listbox1.items) { stringbuilder sb = new stringbuilder(); sb.appendline(stw); //piecing list original format sb_trim = regex.replace(stw, @"[$,]", ""); sb_trim = regex.replace(sb_trim, @"[.][0-9]+", ""); sb_trim = regex.replace(sb_trim, @"\s", ","); sw.writeline(sb_trim); } } }
you can 2 replace's
//let stw "john smith $100,000.00 m" sb_trim = regex.replace(stw, @"\s+\$|\s+(?=\w+$)", ","); //sb_trim becomes "john smith,100,000.00,m" sb_trim = regex.replace(sb_trim, @"(?<=\d),(?=\d)|[.]0+(?=,)", ""); //sb_trim becomes "john smith,100000,m" sw.writeline(sb_trim);
Comments
Post a Comment