Regex removing double/triple comma in string

前端 未结 5 1804
天命终不由人
天命终不由人 2021-02-15 01:00

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:08

    Actually, you can do it without any Trim calls.

    text = Regex.Replace(text, "^,+|,+$|(?<=,),+", "");
    

    should do the trick.

    The idea behind the regex is to only match that, which we want to remove. The first part matches any string of consecutive commas at the start of the input string, the second matches any consecutive string of commas at the end, while the last matches any consecutive string of commas that follows a comma.

提交回复
热议问题