-->

Regex removing double/triple comma in string

2020-07-07 03:54发布

问题:

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

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

The point is, I don't know in advance how many commas separates the words.
Is there a regex I could use in C# that could help me resolve this problem?

回答1:

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(',');


回答2:

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,}.



回答3:

a simple solution without regular expressions :

string items = inputString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(",", items);


回答4:

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.



回答5:

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  


标签: c# regex