Regex-How to remove a comma that is between "and"?

How to remove, (comma), which is between "(double inverted comma) and" (double inverted comma). As it is "a","b","c","d,d","e","f", and then from this, between "and" there is one comma that needs to be removed, and after removing this comma, should it be "a","b","c","dd","e","f"using a regular expression in C #?

EDIT . I forgot to point out that between quotation marks, such as there "a","b","c","d,d,d","e","f"may be a double comma for which the regex doesn't work. and there can be any number of commas between quotation marks.

And there may be a type string a,b,c,"d,d",e,f, then the result should be like a,b,c,dd,e,f, and if the type string a,b,c,"d,d,d",e,f, the result should be like a,b,c,ddd,e,f.

+3
source share
6 answers

Assuming the input is as simple as your examples (i.e. not complete CSV data), this should do it:

string input = @"a,b,c,""d,d,d"",e,f,""g,g"",h";
Console.WriteLine(input);

string result = Regex.Replace(input,
    @",(?=[^""]*""(?:[^""]*""[^""]*"")*[^""]*$)",
    String.Empty);
Console.WriteLine(result);

conclusion:

a, b, c, "d, d, d", e, f, "g, g", h
a, b, c, "ddd", e, f, "gg", h

A regular expression matches any comma followed by an odd number of quotes.


EDIT: if fields instead of apostrophes ( ') are specified instead of quotes ( "), then the technique will be exactly the same, except that you do not need to hide quotes:

string input = @"a,b,c,'d,d,d',e,f,'g,g',h";
Console.WriteLine(input);

string result = Regex.Replace(input,
    @",(?=[^']*'(?:[^']*'[^']*')*[^']*$)",
    String.Empty);
Console.WriteLine(result);

If some fields were quoted with apostrophes and others with quotes, a different approach would be required.


EDIT: , , , , ( ):

@",(?=[^']*'(?:[^']*'[^']*')*[^']*$|[^""]*""(?:[^""]*""[^""]*"")*[^""]*$)"

, , 'a,a',"b,b". , , '9" Nails' (sic) "Kelly Heroes". CSV ( ), , .: D

+10

โ€” , , "". , csv , , csv : csv "".

, csv. , , csv, , , . , , , . , .

(, , ) - CSV. , (FastCSV Linq-to-CSV), ( ) .Net Framework (Microsoft.VisualBasic.TextFieldParser), . , , .

, , . , , , csv, , , . , , , , , , , , , , .

+5

:

var result = Regex.Replace(yourString, "([a-z]),", "$1");

, .

+1
var input = "\"a\",\"b\",\"c\",\"d,d\",\"e\",\"f\"";
var regex = new Regex("(\"\\w+),(\\w+\")");
var output = regex.Replace(input,"$1$2");
Console.WriteLine(output);

, , \w.

+1

This should be very simple using Regex.Replace and a callback:

string pattern = @"
""      # open quotes
[^""]*  # some not quotes
""      # closing quotes
";
data = Regex.Replace(data, pattern, m => m.Value.Replace(",", ""),
    RegexOptions.IgnorePatternWhitespace);

You can even make a small modification to allow escaped quotes (here I have it \", and the comments explain how to use "":

string pattern = @"
\\.     # escaped character (alternative is be """")
|
(?<Quotes>
    ""              # open quotes
    (?:\\.|[^""])*  # some not quotes or escaped characters
                      # the alternative is (?:""""|[^""])*
    ""              # closing quotes
)
";
data = Regex.Replace(data, pattern,
            m => m.Groups["Quotes"].Success ? m.Value.Replace(",", "") : m.Value,
            RegexOptions.IgnorePatternWhitespace);

If you need one quote, replace everything ""in the template with one '.

+1
source

Something like the following, maybe?

"(,)"

-1
source

Source: https://habr.com/ru/post/1796443/


All Articles