title: iterating over boost regex_iterator results
body: <p>I need some help to understand how to iterate over the search results from a boost::sregex_iterator.  Basically I am passing in a ';' delimited set of IP addresses from the command line, and I would like to be able to process each IP address in turn using a boost::sregex_iterator.  </p>
  
  <p>The code below demonstrates what I am trying to do and also shows a workaround using the <strong>workingIterRegex</strong> - however the workaround limits the richness of my regular expression.  I tried modifying the <strong>nonworkingIterRegex</strong> however it only returns the last IP address to the lambda.</p>
  
  <p>Does anyone know how I can loop over each IP address individually without having to resort to such a hacked and simplistic workingIterRegex.</p>
  
  <p>I found the following <a href="http://www.cs.ucr.edu/~cshelton/courses/cppsem/regex2.cc" rel="nofollow noreferrer">http://www.cs.ucr.edu/~cshelton/courses/cppsem/regex2.cc</a> to show how to call the lambda with the individual sub matches.</p>
  
  <p>I also used the example in <a href="https://stackoverflow.com/questions/7301649/how-do-i-loop-through-results-from-stdregex-search">looping through sregex_iterator results</a> to get access to the sub matches however it gave similar results.</p>
  
  <p>After using the workingIterRegex the code prints out the IP addresses one per line</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;boost/regex.hpp&gt;
  ...
  std::string errorMsg;
  std::string testStr("192.168.1.1;192.168.33.1;192.168.34.1;192.168.2.1");
  static const boost::regex nonworkingIterregex (
      "((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3});?)+",
      boost::regex_constants::icase);
  boost::smatch match;
  if (boost::regex_match(testStr, match, nonworkingIterregex)) {            
      static const boost::regex workingIterRegex("[^;]+");
      std::for_each(boost::sregex_iterator(
          begin(iter-&gt;second), end(iter-&gt;second), workingIterRegex),
          boost::sregex_iterator(),
          [](const boost::smatch &amp;match){ 
              std::cout &lt;&lt; match.str() &lt;&lt; std::endl;
          }
      );
  
      mNICIPAddrs = UtlStringUtils::tokenize(iter-&gt;second, ";");
      std::string errorMsg;
  } else {
      errorMsg = "Malformed CLI Arg:" + iter-&gt;second;
  }
  if (!errorMsg.empty()) {
      throw std::invalid_argument(errorMsg);
  }
  ...
  </code></pre>
  
  <p>After some experimentation I found that the following worked - but I am not sure why the c</p>