title: STD Regular Expressions in c++
body: <p>I am using the <code>std::regex</code> but there has to be something I am doing wrong. I have strings like this: </p>
  
  <blockquote>
    <p>127.27.18.4_2.125.2.365.24.2_10</p>
  </blockquote>
  
  <p>and I have to take the first field before "_" that it is an ip, the second that it is a snmp OID and the last one the polling time. These are my patterns:</p>
  
  <pre><code>\\d+\\.\\d+\\.\\d+\\.\\d+_(\\d+\\.)(\\d+\\.*)+_\\d+
  \\d+\\.\\d+\\.\\d+\\.\\d+
  _(\\d+\\.)(\\d+\\.*)+_
  </code></pre>
  
  <p>For the last one, I just serach for a digit starting from the end of the string.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>string generalCast = "\\d+\\.\\d+\\.\\d+\\.\\d+_(\\d+\\.)(\\d+\\.*)+_\\d+";
  string input = "";
  string ip = "";
  
  if (regex_match(input, regex(generalCast)))
  {
      string ipCast = "\\d+\\.\\d+\\.\\d+\\.\\d+";
      string oidCast = "_(\\d+\\.)(\\d+\\.*)+_";
  
      string oid = "";
      string polling = "";
  
      smatch matches;
      if (regex_match(input, matches, regex(ipCast)))
      {
          ip = matches[0];
          oid = "Error, bad OID: " + input;
  
          if (regex_match(input, matches, regex(oidCast)))
          {
              oid = regex_replace((string)matches[0], regex("_"), "");
  
              regex_match(input.cend(), input.cbegin(), matches, regex("\\d+"));
              if (matches.length() &gt; 0)
              {
                  polling = matches[0];
              }
          }
      }
  }
  </code></pre>
  
  <p>The case is that the program go ahead from the first if, but in the second when I ask only for the ip, it does not match.
  i am used to work with regular expression in .Net and things like these work.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I need to use RE because I need to know when a field is coming wrong. Before I was using: stringstream and getlines.</p>