Reputation: 41
I want to remove all double quotes using regex in CSV file if its coming between double quotes. Currently I am using following regex to do this : only from numbers and not from alphabets.. The input is
"000027679","ROMANO","CRYSTAL","S","FT","19990706","19990706","A",,"006901",
I tried this regex expression
string newcontent = Regex.Replace(contents, @"[\""]", "");
but this is removing all the double quotes which I don't want. I want to remove only double quotes from numbers.
Upvotes: 4
Views: 741
Reputation: 115222
You can do it with captured group in regex, use regex "(\d+)"
and replace it with captured value
string contents="\"000027679\",\"ROMANO\",\"CRYSTAL\",\"S\",\"FT\",\"19990706\",\"19990706\",\"A\",,\"006901\"";
string newcontent = Regex.Replace(contents,@"""(\d+)""", "$1");<hr>
OUTPUT:
000027679,"ROMANO","CRYSTAL","S","FT",19990706,19990706,"A",,006901
Upvotes: 3
Reputation: 19149
Use positive lookahead and lookbehind. means that double quotes only matches when the preceding characters or next characters are digits.
string newcontent = Regex.Replace(contents, @"(?<=\d+\d)[""]|[""](?=\d+\d)", "");
Upvotes: 0