How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell?

You can do this using Select-String in PowerShell 2.0 like so:

Select-String F\d\d -input $string -AllMatches | Foreach {$_.matches}

A while back I had asked for a -matchall operator on MS Connect and this suggestion was closed as fixed with this comment:

"This is fixed with -allmatches parameter for select-string."


I suggest using this syntax as makes it easier to handle your array of matches:

$string = "blah blah F12 blah blah F32 blah blah blah" ;
$matches = ([regex]'F\d\d').Matches($string);
$matches[1].Value; # get matching value for second occurance, F32