Good Afternoon,
I think I am a little over my head in this particular task. I am trying to create a regex match function to input a command, and split up the command name, parameters, and parameter values.
New-Variable -Name Something -Force Results should be
- New-Variable
- -Name
- Something
- -Force
I have come up with this so far, but it only captures the 1st argument set.
Bonus: Is there any way to make all the matches after the command incrementally named? Say Parameter1, Value1, Parameter2, Value2, etc?
^(?P<Command>[a-zA-Z]+-[a-zA-Z]+)(?: +)((-\S+)(?: |:|=)(.*){0,1})(?: +)
I had no idea the PowerShell parser even existed but this is awesome. This is the code I settled on. Thank you guys for the help!
#Split up the command argument, needed to pull useful information from the command.
New-Variable -force -Name SplitCommand -Value ([System.array]$Null)
$null = [System.Management.Automation.Language.Parser]::ParseInput($Command, [ref]$SplitCommand,[ref]$Null)
$SplitCommand = $SplitCommand.where({-NOT [String]::IsNullOrEmpty($_.text)}).text