[SOLVED] Splitting a string on spaces but ignore section between double quotes

Issue

How is it possible to split this string:

$Arguments = '/NOGUI /DATE /EXPAND 4 /SIZEUNIT 3 /SORTTYPE 0 /EXCEL "S:\Test\Brecht\Log Test\Report 13.xlsx" /SHEETNAME "Data set" /SCANPATH "S:\My Folder"'

into the following array:

/NOGUI
/DATE
/EXPAND
4
/SIZEUNIT
3
/SORTTYPE
0
/EXCEL
"S:\Test\Brecht\Log Test\Report 13.xlsx"
/SHEETNAME
"Data set"
/SCANPATH
"S:\My Folder"

When using $Argument.split(' ') it also splits the double quoted strings, which is not desired. On the ScriptingGuy’s blog, they explain what is possible. But I can’t seem to find a way to ignore the strings between double quotes.

Solution

$Arguments = '/NOGUI /DATE /EXPAND 4 /SIZEUNIT 3 /SORTTYPE 0 /EXCEL "S:\Test\Brecht\Log Test\Report 13.xlsx" /SHEETNAME "Data set" /SCANPATH "S:\My Folder"'

$splitString = [regex]::Split( $Arguments, ' (?=(?:[^"]|"[^"]*")*$)' )
$splitString

Output:

/NOGUI
/DATE
/EXPAND
4
/SIZEUNIT
3
/SORTTYPE
0
/EXCEL
"S:\Test\Brecht\Log Test\Report 13.xlsx"
/SHEETNAME
"Data set"
/SCANPATH
"S:\My Folder"  

Answered By – Ketanbhut

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *