title: How do you capture a group with regex?
body: <p>I'm trying to extract a string from another using regex.
  I'm using the POSIX regex functions (<code>regcomp, regexec</code> ...), and I fail at capturing a group ...</p>
  
  <p>For instance, let the pattern be something as simple as <code>"MAIL FROM:&lt;(.*)&gt;"</code><br>
  (with <code>REG_EXTENDED</code> cflags)</p>
  
  <p>I want to capture everything between '&lt;' and '>'</p>
  
  <p>My problem is that <code>regmatch_t</code> gives me the boundaries of the whole pattern (MAIL FROM:&lt;...>) instead of just what's between the parenthesis ...</p>
  
  <p>What am I missing ?</p>
  
  <p>Thanks in advance,</p>
  
  <p><strong>edit: some code</strong></p>
  
  <pre><code>#define SENDER_REGEX "MAIL FROM:&lt;(.*)&gt;"
  
  int main(int ac, char **av)
  {
    regex_t regex;
    int status;
    regmatch_t pmatch[1];
  
    if (regcomp(&amp;regex, SENDER_REGEX, REG_ICASE|REG_EXTENDED) != 0)
      printf("regcomp error\n");
    status = regexec(&amp;regex, av[1], 1, pmatch, 0);
    regfree(&amp;regex);
    if (!status)
        printf(  "matched from %d (%c) to %d (%c)\n"
               , pmatch[0].rm_so
               , av[1][pmatch[0].rm_so]
               , pmatch[0].rm_eo
               , av[1][pmatch[0].rm_eo]
              );
  
    return (0);
  }
  </code></pre>
  
  <p><strong>outputs:</strong></p>
  
  <pre><code>$./a.out "012345MAIL FROM:&lt;abcd&gt;$"
  matched from 6 (M) to 22 ($)
  </code></pre>
  
  <p><strong>solution:</strong></p>
  
  <p>as RarrRarrRarr said, the indices are  indeed in <code>pmatch[1].rm_so</code> and <code>pmatch[1].rm_eo</code><br>
  hence <code>regmatch_t pmatch[1];</code> becomes <code>regmatch_t pmatch[2];</code><br>
  and <code>regexec(&amp;regex, av[1], 1, pmatch, 0);</code> becomes <code>regexec(&amp;regex, av[1], 2, pmatch, 0);</code>  </p>
  
  <p>Thanks :)</p>