title: Regular expression only finding first match
body: <p>I'm working on something that is similar to other designs I've done, but for some reason, it's only finding the first key/value pair, whereas other ones found all of them.  It looks good in regex101.com, which is where I typically test these.</p>
  <p>I'm parsing c++ code to get what I need for a reference spreadsheet for error tracking across a system, and results go into a spreadsheet, or is used as a key to lookup info in another file.  I do something similar for about 20 files, plus there's other data coming from a sql query, or access/mdb file.  The data for this file looks like this:</p>
  <pre><code>m_ErrorMap.insert(make_pair(
      MAKEWORD(scError,seFatal),
      HOP_FATAL_ERROR ));
  m_ErrorMap.insert(make_pair(
      MAKEWORD(scError,seNotSelected), 
      HOP_NOT_SELECTED));
  m_ErrorMap.insert(make_pair(
      MAKEWORD(scError,seCoverOpen),
      HOP_COVER_OPEN ));
  m_ErrorMap.insert(make_pair(
      MAKEWORD(scError,seLeverPosition),
      HOP_LEVER_POSITION ));
  m_ErrorMap.insert(make_pair(
      MAKEWORD(scError,seJam),
      HOP_JAM ));
  </code></pre>
  <p>I read this as a string from the file (looks good), and feed it into this Function as $fileContent:</p>
  <pre><code>Function Get-Contents60{
    [cmdletbinding()]
    Param ([string]$fileContent)
    Process
    {
       
              #m_ErrorMap.insert(make_pair(
              #MAKEWORD(scError,seJam),
              #HOP_JAM ));
  
       # construct regex
       switch -Regex ($fileContent -split '\r?\n') {  #this is splitting on each line               test regex with https://regex101.com/
        'MAKEWORD[&quot;(&quot;][\w]+,(\w+)[&quot;)&quot;],' { #seJam
            # add relevant key to key collection
            $keys = $Matches[1] } #only match once
            ',(HOP.*?)[\s]' { #  HOP_JAM
            # we've reached the relevant error, set it for all relevant keys
            foreach($key in $keys){
                Write-Host &quot;60 key: $key&quot;
                Write-Host &quot;Matches[0]: $($Matches[0]) Matches[1]: $($Matches[1])&quot;
                $errorMap[$key] = $($Matches[1])
                Write-Host &quot;60 key: $key ... value: $($errorMap[$key])&quot;
            }
        }
        'break' {
            # reset/clear key collection
            $keys = @()
        }    
      }#switch
  
  
      #Write-Host &quot;result:&quot; $result -ForegroundColor Green
      #$result;
      return $errorMap
  
  
       
    }#End of Process
  }#End of Function
  </code></pre>
  <p>I stepped through it in VSCode, and its finding the first key/value pair, and after that it's not finding anything.  I looked at it in regex101.com, and it's finding line endings/breaks, and the MAKEWORD regex and HOP regex are finding what they should on each line it should.</p>
  <p>I'm not sure if the issue is that they aren't all in the same line, and maybe I need to change it so it doesn't break on newline and breaks on something else for each key/value pair?  I'm a little fuzzy on this.</p>
  <p>I'm using powershell 5.1, and VSCode.</p>
  <p>Update:</p>
  <p>I modified Theo's answer and it worked great.  I had simplified the class name from m_HopErrorMap to m_ErrorMap for this question, and the regular expression was grabbing that for each one. I modified that slightly, and Theo's works.</p>
  <pre><code>function Get-Contents60{
      [cmdletbinding()]
      Param ([string]$fileContent)
  
      # create an ordered hashtable to store the results
      $errorMap = [ordered]@{}
      # process the lines one-by-one
      switch -Regex ($fileContent -split '\r?\n') {
          'MAKEWORD\([^,]+,([^)]+)\),' { # seJam, seFatal etc.
              $key = $matches[1]
          }
          '(HOP_[^)]+)' {
              $errorMap[$key] = $matches[1].Trim()
          }
      }
      # output the completed data as object
      [PsCustomObject]$errorMap
      return $errorMap
  }
  </code></pre>