Regular expression matching in PowerShell

后端 未结 5 843
野性不改
野性不改 2021-01-01 13:41

Is there an elegant one-liner for doing the following?

$myMatch = \"^abc(.*)\"
$foo -match $myMatch
$myVar = $matches[1]

I\'m interested in

5条回答
  •  无人及你
    2021-01-01 14:10

    Changed answer after clarification:

    True one-liner:

    $myvar = ($foo | ? {$_ -match $myMatch} | select @{L="Matches";E={$matches[1]}}).matches
    

    Fake one-liner:

    $foo | ? {$_ -match $myMatch} ; $myvar = $matches[1]
    

提交回复
热议问题