Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove double quotes inside double quotes

Tags:

c#

vb.net

I have a problem and cannot figure out what to do.

Question: I need to remove a double quote, inside double quotes


String example:

"MIKE YANICK","412 A AVE "E"," ","NADIEN PA"," ","190445468"

As you can see, the letter E inside "412 A AVE "E" has an extra double quote.


I need to remove it.

Here is what my outcome should be:

"MIKE YANICK","412 A AVE E"," ","NADIEN PA"," ","190445468"


  • I cannot use expressions because the pattern changes every time.
  • The string.replace will not work, because you would need to hard code the values
  • Reading values in between double quotes does not work, because it gets thrown off by the quote in the middle

Please help...

like image 317
Internet Engineer Avatar asked Sep 16 '25 10:09

Internet Engineer


2 Answers

You could use a regular expression like this:

(?<!(^|,))"(?!(,|$))

This will match any double quote (") that isn't proceeded by the start of the string, or a comma, and isn't followed by a comma or the end of the string.

like image 84
Matt Burland Avatar answered Sep 19 '25 00:09

Matt Burland


This works with your example:

Regex.Replace("\"MIKE YANICK\",\"412 A AVE \"E\",\" \",\"NADIEN PA\",\" \",\"190445468\"",
    "(?<=\")([^,]*)(?=\")",
    m => m.Value.Replace("\"", string.Empty)) ;

Output:

"MIKE YANICK","412 A AVE E"," ","NADIEN PA"," ","190445468"
like image 45
BlackBear Avatar answered Sep 19 '25 00:09

BlackBear