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

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


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...