title: why regexec() in posix c always return the first match,how can it return all match positions only run once?
body: <p>Now when I want to return all match positions in str, such as:</p>
  
  <pre><code>abcd123abcd123abcd
  </code></pre>
  
  <p>Suppose I want to get all "abcd", I must use regexec(),get the first position:0, 3, then I will use:</p>
  
  <pre><code>123abcd123abcd
  </code></pre>
  
  <p>as the new string to use regexec() again, and so on.
  I read the manual about regexec(), it says:</p>
  
  <pre><code>int regexec(const regex_t *preg, const char *string, size_t nmatch,
                 regmatch_t pmatch[], int eflags);
  nmatch and pmatch are used to provide information regarding the location of any 
  matches.
  </code></pre>
  
  <p>but why doesn't this work?
  This is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;regex.h&gt;
  
  int main(int argc, char **argv)
  {
     int i = 0;
     int res;
     int len;
     char result[BUFSIZ];
     char err_buf[BUFSIZ];
     char* src = argv[1];  
  
     const char* pattern = "\\&lt;[^,;]+\\&gt;";
     regex_t preg;
  
     regmatch_t pmatch[10];
  
     if( (res = regcomp(&amp;preg, pattern, REG_EXTENDED)) != 0)
     {
        regerror(res, &amp;preg, err_buf, BUFSIZ);
        printf("regcomp: %s\n", err_buf);
        exit(res);
     }
  
     res = regexec(&amp;preg, src, 10, pmatch, REG_NOTBOL);
     //~ res = regexec(&amp;preg, src, 10, pmatch, 0);
     //~ res = regexec(&amp;preg, src, 10, pmatch, REG_NOTEOL);
     if(res == REG_NOMATCH)
     {
        printf("NO match\n");
        exit(0);
     }
     for (i = 0; pmatch[i].rm_so != -1; i++)
     {
        len = pmatch[i].rm_eo - pmatch[i].rm_so;
        memcpy(result, src + pmatch[i].rm_so, len);
        result[len] = 0;
        printf("num %d: '%s'\n", i, result);
     }
     regfree(&amp;preg);
     return 0;
  }
  
  ./regex 'hello, world'
  </code></pre>
  
  <p>the output:</p>
  
  <pre><code>num 0: 'hello'
  </code></pre>
  
  <p>this is my respect outputs:</p>
  
  <pre><code>num 0: 'hello'
  num 1: 'world'
  </code></pre>