Regex removing double/triple comma in string

前端 未结 5 848
一生所求
一生所求 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:08

    Here is my effort:

    //Below is the test string 
    string test = "YK     002       10        23           30         5       TDP_XYZ  "
    private static string return_with_comma(string line)
        {
            line = line.TrimEnd();
            line = line.Replace("  ", ",");
            line = Regex.Replace(line, ",,+", ",");
            string[] array;
            array = line.Split(',');
            for (int x = 0; x < array.Length; x++)
            {
                line += array[x].Trim();
            }
            line += "\r\n";
            return line;
        }
     string result = return_with_comma(test);
     //Output is
     //YK,002,10,23,30,5,TDP_XYZ  
    
    0 讨论(0)
  • 2021-02-15 01:10

    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.

    0 讨论(0)
  • 2021-02-15 01:23

    Search for ,,+ and replace all with ,.

    So in C# that could look like

    resultString = Regex.Replace(subjectString, ",,+", ",");
    

    ,,+ means "match all occurrences of two commas or more", so single commas won't be touched. This can also be written as ,{2,}.

    0 讨论(0)
  • 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(',');
    
    0 讨论(0)
  • 2021-02-15 01:31

    a simple solution without regular expressions :

    string items = inputString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    string result = String.Join(",", items);
    
    0 讨论(0)
提交回复
热议问题