【问题标题】:Regex to take command and split up Command, Arguments, and Argument Values正则表达式接受命令并拆分命令、参数和参数值
【发布时间】:2021-08-04 18:15:10
【问题描述】:

下午好,

我认为我在这项特殊任务中有点过头了。我正在尝试创建一个正则表达式匹配函数来输入命令,并拆分命令名称、参数和参数值。

新变量-命名某事-Force 结果应该是

  1. 新变量
  2. -姓名
  3. 某事
  4. -强制

到目前为止,我已经想出了这个,但它只捕获了第一个参数集。

奖励:有没有办法在命令增量命名之后使所有匹配项?说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


    【解决方案1】:

    不要为此使用正则表达式 - 改用内置解析器:

    # Prepare command to parse
    $command = 'New-Variable -Name Something -Force'
    
    # Parse command invocation - Parser will return an Abstract Syntax Tree object
    $parserErrors = @()
    $AST = [System.Management.Automation.Language.Parser]::ParseInput($command, [ref]$null, [ref]$parserErrors)]
    
    if($parserErrors){
        # error encountered while parsing script
    }
    else {
        # No errors, let's search the AST for the first command invocation
        $CommandInvocation = $AST.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $false)
    
        # Get the string representation of each syntax element in the command invocation
        $elements = $CommandInvocation.CommandElements |ForEach-Object ToString
    }
    

    根据您的输入 (New-Variable -Name Something -Force),生成以下字符串:

    PS ~> $elements
    New-Variable
    -Name
    Something
    -Force
    

    注意:与: 紧密绑定的参数将被解释为单个联合语法元素,例如。 'Get-Thing -Name:nameOfThing' 只会产生两个字符串(Get-Thing-Name:nameOfThing) - 如果您希望将它们拆分为单独的字符串,请在将它们转换为字符串之前考虑到这一点:

    $elements = $CommandInvocation.CommandElements |ForEach-Object {
      if($null -ne $_.Argument){
        # tightly bound argument, output both separately
        "-$($_.ParameterName)"
        $_.Argument
      } else {
        # just the parameter name, output as-is
        $_
      }
    } |ForEach-Object ToString
    

    【讨论】:

    • 尽管使用了多年并且经常在其中编写代码,但我仍然对 Powershell 仍然一无所知,这总是让我感到惊讶。泰。
    • @NickW。兔子洞很深^_^不客气!
    • 刚刚做了一些测试,我认为不需要第二部分,它似乎处理“:”没有它就好了。
    • @NickW。如果您想要 -param:arg 表示为单个字符串,则不需要它 - 但如果您希望参数名称和参数严格分开,它是:-)
    • 我的意思是当我通过-Parameter:value 时,它确实将它们分开。
    【解决方案2】:

    补充 Mathias R. Jessen's helpful answer 采用简化的解决方案,假设:

    • 您只是对表示命令名称及其参数的 字符串 数组感兴趣。

    • 该字符串表示语法上有效的命令,即您不必担心错误处理。

    $stringToParse = 'New-Variable -Name Something -Force'
    
    $tokens = $null # Initialize the output variable.
    # Parse the string as a PowerShell command.
    $null = [System.Management.Automation.Language.Parser]::ParseInput(
      $stringToParse, 
      [ref] $tokens,  # will receive a collection of tokens
      [ref] $null     # would receive a collection of parsing errors; unused here
    )
    
    $tokenStrings = $tokens.Text -ne '' # -ne '' ignores the end-of-input token
    

    $tokenStrings 然后包含一个带有逐字元素的字符串数组

    New-Variable-NameSomething-Force

    另见:

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2016-07-14
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多