RegEx Named Groups with Select-String
dears
how can named groups via select-string?
example :
text : "aa12aa"
regex : "aa(?<digit>[\d]+)aa"
number "12" changes time, , text "aa" remains no change
how number "12" select-string , regular expression?
thank you
how can named groups via select-string?
example :
text : "aa12aa"
regex : "aa(?<digit>[\d]+)aa"
number "12" changes time, , text "aa" remains no change
how number "12" select-string , regular expression?
thank you
yep, kind of annoying if want use select-string named groups, have run match twice.
$patt = "aa(?<digit>[\d]+)aa"
$content = gc "textfile.txt"
# works
$content | select-string $patt |%{$null = $_.line -match $patt; $matches['digit'] }
# or
$content | select-string $patt |%{[regex]::match($_.line, $patt).groups['digit'].value }
alternate approach not use select-string. the cleanest code, plus think might faster since it's doing match once.
$content |?{$_ -match $patt}|%{$matches['digit']}
thanks,
-lincoln
$patt = "aa(?<digit>[\d]+)aa"
$content = gc "textfile.txt"
# works
$content | select-string $patt |%{$null = $_.line -match $patt; $matches['digit'] }
# or
$content | select-string $patt |%{[regex]::match($_.line, $patt).groups['digit'].value }
alternate approach not use select-string. the cleanest code, plus think might faster since it's doing match once.
$content |?{$_ -match $patt}|%{$matches['digit']}
thanks,
-lincoln
Windows Server > Windows PowerShell
Comments
Post a Comment