6

This question has been asked before but I have been trying to work a solution in powershell but am not getting the desired results.

$line = '1,2,"N",09/04/13,"P09042013ZSD(1,0)","ZSD"'
[string[]] $splitColumns = $line.Split('(,)(?=(?:[^"]|"[^"]*")*$)', [StringSplitOptions]'RemoveEmptyEntries')

When I loop though the split values I am expecting

1
2
"N"
09/04/13
"P09042013ZSD(1,0)"
"ZSD"

But am getting

1
2
N
09/04/13
P09042013ZSD
1
0
ZSD

I have tested the regex using http://regexhero.net/tester/ (Split) with ExplicitCapture set and it returns the desired results.

Working solution

$RegexOptions = [System.Text.RegularExpressions.RegexOptions]
$csvSplit = '(,)(?=(?:[^"]|"[^"]*")*$)'

$splitColumns = [regex]::Split("StringHere", $csvSplit, $RegexOptions::ExplicitCapture)
David
  • 15,150
  • 15
  • 61
  • 83

1 Answers1

9

[string].split() method doesn't accept regex on split but just [char[]] or [string[]].

You can try like this:

 $line -split ',(?=(?:[^"]|"[^"]*")*$)' 

powershell -split accept regex for splitting text

Using .net you can do it like this:

[regex]::Split( $line , ',(?=(?:[^"]|"[^"]*")*$)' )
CB.
  • 58,865
  • 9
  • 159
  • 159
  • I also tried -split. It added the , as extra lines i.e. 1 , 2 would be 3 items. Is there anyway to remove these , entries such as how StringSplitOptions]'RemoveEmptyEntries' worked? – David Apr 10 '13 at 13:52
  • @DavidLiddle Have you tried my code? I've removed the capture on `,` – CB. Apr 10 '13 at 13:55
  • I got it working with the [regex]::Split and RegexOptions.ExplicitCapture. – David Apr 10 '13 at 13:56
  • If you remove the capture group on `,` you can avoid the `regexoptions` – CB. Apr 10 '13 at 13:58