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.

here start ( string trailing commas )

here end string need

you can simplify regular expression eliminating capture groups, replacing purpose of latter 1 lookahead:

var regex = new regex(@",\s*(?=\}|\])"); return regex.replace(jsoncode, " "); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -