c# - Regex performance issue on a really big string -
right new using regexes appreciate help.
i have large string (i parsing as3 file json) , need locate trailing commas out there in objects..
this regex using
public static string trimtraillingcommas(string jsoncode) { var regex = new regex(@"(.*?),\s*(\}|\])", (regexoptions.multiline)); return regex.replace(jsoncode, m => string.format("{0} {1}", m.groups[1].value, m.groups[2].value)); }
the problem it's slow. without using in string time complete program : 00:00:00.0289668
, : 00:00:00.4096293
could suggest improved regex or algorithm faster replacing trailing commas.
you can simplify regular expression eliminating capture groups, replacing purpose of latter 1 lookahead:
var regex = new regex(@",\s*(?=\}|\])"); return regex.replace(jsoncode, " ");
Comments
Post a Comment