ted ted - 3 months ago 12
PowerShell Question

Regular expression matching in PowerShell

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

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


I'm interested in the
$myVar
variable...

Answer

Just use -replace:

$foo = 'abcDEF'
$myMatch = "^abc(.*)"
$myVar = $foo -replace $myMatch,'$1'
#$myVar contains DEF