vishnu chander
vishnu chander

Reputation: 41

Remove Double Quotes only from Numbers

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

Answers (2)

Pranav C Balan
Pranav C Balan

Reputation: 115222

You can do it with captured group in regex, use regex "(\d+)" and replace it with captured value


CODE:

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

M.kazem Akhgary
M.kazem Akhgary

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

Related Questions