I am having a csv file, quotes around each field.
there are some fields, which can have multiple double quotes inside them. I want to escape each of them with additional double quote.
","ABC "XYZ" PQRS","
","ABC "XYZ"","
","ABC "A" "B" TEST","
","ABC 2.5" "C" Test","
I took help from the link and able to cover for scenarios with single double quote inside content, using regular expression [regex]$r='(","[^"]+"[^"]+?",")'
. But, stuck up in the cases, where there are multiple double quotes inside content.
[regex]$r='(","[^"]+"[^"]+"",")' # Not working
get-content C:\Projects\MyProject\testRegexFordoublequotes.csv | foreach {
#save each line to a variable to make it easier to track
$line=$_
#look for a regex match
$find=$r.matches($line)
if ($find[0].Success) {
foreach ($match in $find) {
#the original string we matched on
$found=$match.value
#replace the substring
$replace= '","'+ $found.Trim('","').Replace('""','"').Replace('"','""')+ '","'
#replace the full string and write to the pipeline
$line -replace $found,$replace
} #foreach
} #if
else {
#no match so write the line to pipeline
$line
}
} | Set-Content C:\Projects\MyProject\modified.csv -Force
Can you please help me in defining regex which will be helpful for multiple double quotes inside field.