32

Let's say I have the string "blah blah F12 blah blah F32 blah blah blah" and I want to match the F12 and F32, how would I go about capturing both to the Powershell magic variable $matches?

If I run the following code in Powershell:

$string = "blah blah F12 blah blah F32 blah blah blah"
$string -match "F\d\d"

The $matches variable only contains F12

I also tried:

$string -match "(F\d\d)"

This time $matches had two items, but both are F12

I would like $matches to contain both F12 and F32 for further processing. I just can't seem to find a way to do it.

All help would be greatly appreciated. :)

1
  • anyone wondering why -match returns two F12 results in the second attempt, it's because F12 is both the result of the whole match expression AND the result of the first match group in the expression (the result of the parentheses... May 20, 2016 at 14:46

4 Answers 4

59

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."

6
  • 1
    So how do you reference the captured values, from Select-String -AllMatches? May 8, 2012 at 19:38
  • 11
    @NathanHartley $string | Select-String "(F\d\d)" -AllMatches | % matches | % Value
    – Keith Hill
    May 8, 2012 at 19:53
  • Definitely not ideal, since it won't match across new lines.
    – lordcheeto
    Feb 7, 2018 at 19:11
  • 1
    @lordcheeto - what if you tried a pattern like: "(?m)(F\d\d)" - the initial (?m) tells regex engine to do multi-line match... May 11, 2018 at 17:47
  • 1
    @user1390375 Doesn't appear to work that way from the Select-String command.
    – lordcheeto
    May 12, 2018 at 6:13
31

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
1
  • 1
    I wouldn't use the reserved variable name $matches, but an own variable like $m. And if capturing groups are present, to show all values of, for example group 1, use: $m | % {$_.groups[1].value}
    – jamacoe
    Aug 11, 2022 at 5:57
3

I see 2 scenarios that are handled differently:

  1. extracting all matches of a single pattern
  2. extracting single match of multiple patterns

1. extract all matches of one pattern: select-string + -allmatches

  • e.g. regex: (?<=jobs).*
  • counter-intuitive but you need to use Select-String to handle this like I am to get ids of nomad jobs from the output exemplified below
$m = "Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e183af-8243-11eb-a2af-0a58a9feac2a
08:23
Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e130e9-8243-11eb-a2af-0a58a9feac2a"
$r = "(?<=jobs/).*"
$l = Select-String $r -InputObject $m -AllMatches | 
    Foreach {$_.matches.Value}
20e183af-8243-11eb-a2af-0a58a9feac2a
20e130e9-8243-11eb-a2af-0a58a9feac2a
$l[0]
>>> 20e183af-8243-11eb-a2af-0a58a9feac2a

2. extract a single/first match of one/multiple patterns: capture groups and $Match[]

▶ $s = "Hello World from Mr Pavol"
▶ $r = "(World).*(Pavol)"
▶ $s -match $r
True
▶ $Matches
Name                           Value
----                           -----
2                              Pavol
1                              World
0                              World from Mr Pavol
1
  • Easy to learn illustration Feb 13 at 5:31
0
$String = @'
MemberProgram PackageID="12345678" ProgramName="Install"/
MemberProgram PackageID="87654321" ProgramName="Install"/
MemberProgram PackageID="21436587" ProgramName="Install"/
MemberProgram PackageID="78563412" ProgramName="Install"/
'@
([regex]'(?<=PackageID=\")\d+(?=\")').Matches($String).value
2
  • Thanks for your answer. To improve the quality and avoid getting downvotes, please add additional commentary about how your answer solves the problem. Simply pasting in some source code is not the best way to answer. Thanks! Sep 24, 2019 at 17:56
  • 2
    Sorry, I am from China, my English is not very good, I don't know how to write a description. Even if this reply is done with the help of Google Translate.T_T
    – BiaoGuo
    Sep 24, 2019 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.