title: Understanding c++ regex by a simple example
body: <p>I wrote the following simple example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;regex&gt;
  
  int main ()
  {
      std::string str("1231");
      std::regex r("^(\\d)");
      std::smatch m;
      std::regex_search(str, m, r);
      for(auto v: m) std::cout &lt;&lt; v &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><strong><a href="http://coliru.stacked-crooked.com/a/0652e59b74536e94" rel="noreferrer">DEMO</a></strong></p>
  
  <p>and got confused by its behavior. If I understood the purpose of the <code>match_result</code> from <a href="http://www.cplusplus.com/reference/regex/match_results/" rel="noreferrer">there</a> correctly, the only one <code>1</code> should have been printed. Actually:</p>
  
  <blockquote>
    <p>If successful, it is not empty and contains a series of sub_match
    objects: the <strong>first sub_match element corresponds to the entire match,</strong>
    and, if the regex expression contained sub-expressions to be matched ([...]) </p>
  </blockquote>
  
  <p>The string passed to the function doesn't match the regex, therefore we should <strong>not</strong> have had <code>the entire match</code>.</p>
  
  <p>What did I miss?</p>