问题
I have a CSV file with a content like:
A,B,C
D,"E,F",G
H,I,"J,K,L"
I need to remove the commas when between quotes (also remove the quotes, but that is not so important):
A,B,C
D,EF,G
H,I,JKL
I looked at PoSh -replace operator but I can't get it to capture multiple group values:
PS >"D,`"E,F`",G" -replace "`"((?:[^,`"]+)\,?)+`"", '$1'
D,F,G
as you can see when the group is repeated, only the last value captured is preserved. Is there a way to do the transformation I want?
https://regex101.com/r/ON1rgp/1/
回答1:
You may define a callback to pass to the Regex::Replace
method where you may just grab the part between quotes and remove all ,
there:
$callback = { param($match) $match.Groups[1].Value.Replace(',','') }
$s = "D,`"E,F`",G"
$rex = [regex]'"([^"]*)"'
$rex.Replace($s, $callback)
The regex is "([^"]*)"
that matches "
, then captures into Group 1 (i.e. the $match.Groups[1].Value
) any zero or more chars other than "
and then matches "
. It will need enhancing in case you have escaped quotes, but the approach will be the same.
回答2:
Here's an alternative to using regex:
Import-Csv your.csv | Foreach-Object {
$_.PSObject.Properties | ForEach-Object { $_.Value = $_.Value -replace ',' }
$_
} | Export-csv your-new.csv -NoTypeInformation
We use Import-CSV
to import the data as a PowerShell object. We then iterate through the value of each property of each row of the CSV to replace the ',' character with nothing. Then we export it to a new CSV.
来源:https://stackoverflow.com/questions/45859551/remove-comma-when-between-quotes-from-batch-or-posh