Regular expression matching in PowerShell

后端 未结 5 814
野性不改
野性不改 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:04

    I use something like the following pretty often:

    ([regex]"^abc(.*)").match($foo).groups[1].value
    

    It's not very PowerShell-y, or at least not very PowerShell V2-y (we're falling back onto the .NET class)... but it's pretty compact.

提交回复
热议问题