Regex removing double/triple comma in string

前端 未结 5 849
一生所求
一生所求 2021-02-15 00:39

I need to parse a string so the result should output like that:

\"abc,def,ghi,klm,nop\"

But the string I am receiving could looks more like tha

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-15 01:26

    You can use the ,{2,} expression to match any occurrences of 2 or more commas, and then replace them with a single comma.

    You'll probably need a Trim call in there too, to remove any leading or trailing commas left over from the Regex.Replace call. (It's possible that there's some way to do this with just a regex replace, but nothing springs immediately to mind.)

    string goodString = Regex.Replace(badString, ",{2,}", ",").Trim(',');
    

提交回复
热议问题