I'm trying to parse a string splitting on commas except when comma is inside quotes:
$teststring = 'Data1,Data2,"Data 3, more", Data 4'
$tempsting = $null
$tempsting = $teststring -split ',(?=([^\"]*\"[^\"]*\")*[^\"]*$)'
Write-Host $teststring
$i =0
Foreach ($object in $tempsting) {
Write-Host $i " " $tempsting[$i]
#Write-Host $tempsting[$i]
$i++
}
Outputs this:
Data1,Data2,"Data 3, more", Data 4
0 Data1
1 Data2,"Data 3, more"
2 Data2
3 "Data 3, more"
4 "Data 3, more"
5 Data 4
What I want is this:
Data1,Data2,"Data 3, more", Data 4
0 Data1
1 Data2
2 "Data 3, more"
3 Data 4