【发布时间】:2021-08-04 18:15:10
【问题描述】:
下午好,
我认为我在这项特殊任务中有点过头了。我正在尝试创建一个正则表达式匹配函数来输入命令,并拆分命令名称、参数和参数值。
新变量-命名某事-Force 结果应该是
- 新变量
- -姓名
- 某事
- -强制
到目前为止,我已经想出了这个,但它只捕获了第一个参数集。
奖励:有没有办法在命令增量命名之后使所有匹配项?说Parameter1,Value1,Parameter2,Value2等?
^(?P<Command>[a-zA-Z]+-[a-zA-Z]+)(?: +)((-\S+)(?: |:|=)(.*){0,1})(?: +)
我什至不知道 PowerShell 解析器存在,但这太棒了。这是我确定的代码。谢谢大家的帮助!
#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
【问题讨论】:
标签: string powershell parsing command-line-arguments