title: Regular Expression in C
body: <p>I'm trying to match a string "123,1234" using regex.h. Following pattern does the job:</p>
  
  <pre><code>"^[0-9]\{1,\},[0-9]\{1,\}$"
  </code></pre>
  
  <p>If I'm giving it as a commandline argument it works fine. But when I use it inside C code it does not work. Probably because identifying backward slashes as escape characters.</p>
  
  <p>Sample Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;regex.h&gt;
  
  int main (int argc, char * argv[]){
      regex_t regex;
      int reti;
      char msgbuf[100];
      char * string, * pattern;
  
      string = "123,1234";
      pattern = "^[0-9]\{1,\},[0-9]\{1,\}$";
  
      if(regcomp(&amp;regex, pattern, 0))
      {
              fprintf(stderr, "Could not compile regex\n");
              exit(107);
      }
  
      if(!(reti = regexec(&amp;regex, string, 0, NULL, 0)))
      {
              printf("MATCH\n");
      }
      else if(reti == REG_NOMATCH)
      {
              printf("NO MATCH\n");
      }
      else
      {
              regerror(reti, &amp;regex, msgbuf, sizeof(msgbuf));
              fprintf(stderr, "Regex match failed: %s\n", msgbuf);
              exit(107);
      }
  
      regfree(&amp;regex);
  
      return 0;
  }
  </code></pre>
  
  <p>How can I solve this?</p>