title: regex for IP address in C
body: <p>I am trying to validate IP address input in C. Here is my code</p>
  
  <pre><code>char* messType;
  
  regex_t regex;
  int regres;
  char msgbuf[100];
  
  /* Compile regular expression */
  regres = 
      regcomp(&amp;regex, 
              "^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
               "([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
               "([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
               "([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$", 0);
  
  if (regres)
  {
      fprintf(stderr, "Could not compile regex\n");
      exit(1);
  }
  regres = regexec(&amp;regex, servIP, 0, NULL, 0);
  
  if( regres == REG_NOMATCH )
  {
      puts("Invalid IP address");
      exit(EXIT_FAILURE);
  }
  else
  {
      regerror(regres, &amp;regex, msgbuf, sizeof(msgbuf));
      fprintf(stderr, "Regex match failed: %s\n", msgbuf);
      exit(1);
  }
  </code></pre>
  
  <p>However, this failed every IPV4 address I entered. </p>