title: How to find all occurrences of a pattern using std::regex_search?
body: <p>I have the following code to parse a bunch of part numbers (basically serial numbers of components of a product) from an arbitrarily formatted file.</p>
  
  <pre><code>auto buildPartNumberRegexString( bool preFlash ) -&gt; std::string
  {
      std::ostringstream patternBuilder;
  
      // The original, raw literal as tested on https://regex101.com/ is:
      //
      // @@PART_NUMBER_POST_FLASH\&lt;\s*(\S+)\s*\,\s*(\d+)\s*\&gt;@@
      //
      // In C++, each backslash needs to be doubled. Alternatively, we could use raw string literals ( R"\w" ).
  
      patternBuilder &lt;&lt; "@@PART_NUMBER_" &lt;&lt; ( preFlash ? "PRE" : "POST" )
          &lt;&lt; "_FLASH\\&lt;\\s*(\\S+)\\s*\\,\\s*(\\d+)\\s*\\&gt;@@";
  
      return patternBuilder.str();
  }
  
  auto parsePartNumberAddresses( const std::string&amp; templateFileContent, bool preFlash ) -&gt; ParamAddressContainer
  {
      const std::regex regEx( buildPartNumberRegexString( preFlash ) );
      std::smatch match;
  
      if ( std::regex_search( templateFileContent, match, regEx ) )
      {
          assert( match.size() &gt; 1 );
          const std::size_t capturedGroups = match.size() - 1;
  
          assert( capturedGroups % 2 == 0 );
          const std::size_t partNumberAddressesFound = capturedGroups / 2;
  
          ParamAddressContainer results;
          results.reserve( partNumberAddressesFound );
  
          std::cerr &lt;&lt; "DEBUG: capturedGroups = " &lt;&lt; capturedGroups &lt;&lt; ", partNumberAddressesFound = " &lt;&lt; partNumberAddressesFound
              &lt;&lt; "\n";
  
          for ( std::size_t i = 0; i &lt; partNumberAddressesFound; ++i )
          {
              const std::size_t paramIdMatchIndex = i * 2 + 1;
              const std::string paramIdString = match.str( paramIdMatchIndex );
              const std::string paramIndexString = match.str( paramIdMatchIndex + 1 );
  
              results.emplace_back( util::string_funcs::fromString&lt; ParamId_t &gt; ( paramIdString ),
                  util::string_funcs::fromString&lt; ParamIndex_t &gt; ( paramIndexString ) );
          }
  
          std::cerr &lt;&lt; "DEBUG: Going to read the following part numbers (" &lt;&lt; ( preFlash ? "pre" : "post" ) &lt;&lt; "-flash):\n\n";
  
          for ( const auto&amp; paramAddress : results )
          {
              std::cerr &lt;&lt; "\t" &lt;&lt; std::hex &lt;&lt; std::noshowbase &lt;&lt; paramAddress.paramId &lt;&lt; std::dec &lt;&lt; "&lt;" &lt;&lt; paramAddress.paramIndex
                  &lt;&lt; "&gt;\n";
          }
  
          return results;
      }
  
      return ParamAddressContainer();
  }
  </code></pre>
  
  <p>I've written the "beautified" regex (i.e. without the double backslashes required to escape the actual backslashes) in the comment in the buildPartNumberRegexString function.</p>
  
  <p>A sample file that I'm using this regex on might look like this:</p>
  
  <pre><code>Component alpha;@@PART_NUMBER_POST_FLASH&lt;F12C,0&gt;@@
  Component beta;@@PART_NUMBER_POST_FLASH&lt;F12C,1&gt;@@
  </code></pre>
  
  <p>I've tested my regex, using that same sample file, on <a href="https://regex101.com/" rel="nofollow">https://regex101.com/</a> and it works exactly as it should, matching both occurrences and extracting the desired match groups. The problem is that, when I try to do the same thing via std::regex it only finds the first match. Now on  <a href="https://regex101.com/" rel="nofollow">https://regex101.com/</a> I had to enable the <strong>g</strong> modifier <em>(global, All matches, don't return on first match)</em> for the regex to find all matches. I'm assuming (hoping) that a similar flag is available for <code>std::regex_search</code>, but the description of the available flags (<a href="http://en.cppreference.com/w/cpp/regex/match_flag_type" rel="nofollow">http://en.cppreference.com/w/cpp/regex/match_flag_type</a>) doesn't seem to list any that meets my requirements. Surely there has to be a way to find more than one occurrence of a pattern, right? Does anyone have an idea?</p>