I'm trying to extract variable names from a given expression using regular expressions. When I have an expression like "0.15*x_val^(res+y_val)" I would like to extract: 'x_val', 'res' and 'y_val'. Using a character class like [a-z_] should do the trick, but I can't seem to get it working when there are multiple matches.

The code that I'm trying is as follows (string isn't an expression, just trying to get it to work..).
  #include <regex.h>


  const char *input= "aaaa+bbbbb+ccccc+ddddd";
  regex_t re;
  size_t erc;
  char *var = "([a-z]+)";
  char new_input[strlen(input)];

  printf("Regex:\t%s\nInput:\t %s\n", var, input);
  /* Compiling regex, using ERE and case-insensitive */
  erc = regcomp(&re, var, REG_EXTENDED|REG_ICASE);
  regmatch_t matches[15]; /* Max number of matches */

  memset(matches, '\0', sizeof(matches));

  /* Matching first element, returns 0 if successful */
  erc = regexec(&re, input, 15, matches, 0);

  int i = 0;
  int prev = 0;

  /* Match will be stored in a struct @ matches[i], where rm_so is start of the match,
   * and rm_eo is end of the match.
   * Both will be set to '-1' if no match is found
  while(erc == 0 && (int)matches[i].rm_so != -1) {
    /* Print match locations in input string */
    printf("Match @ [%d..%d]\n", (int)matches[i].rm_so + prev, (int)matches[i].rm_eo + prev);
    /* Copy input string from end of last match to new input string for next match */
    strncpy(new_input, &input[(int)matches[i].rm_eo + prev], strlen(input) );
    prev += (int)matches[i].rm_eo;
    /* Execute next match with new_input string */
    erc = regexec(&re, new_input, 15, matches, 0);

When executing the code, the result is:

Regex: ([a-z]+)
Input: aaaa+bbbbb+ccccc+ddddd
Match @ [0..4]
Match @ [5..10]

So it matches the first sequence of 'aaaa' and the second sequence of 'bbbbb', but after that, nothing!

Is there anything I'm doing wrong here? It has probably something to do with the way I handle my strings (not really experienced in that, yet) or in the way I execute the regex. I find the documentation about the regexec function lacking in examples on multiple matches and how to handle those..

Any help is very welcome, thanks in advance!