suffit d'appeler la fonction avec une chaine différente:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
int main (void)
{
int err;
regex_t preg;
const char *str="chaine sous chaine sous chaine toto";
const char *str_regex="(sous +chaine)";
err = regcomp (&preg, str_regex, REG_EXTENDED);
if (err == 0)
{
int match;
size_t nmatch = 0;
regmatch_t *pmatch = NULL;
char *buffer;
nmatch = preg.re_nsub;
printf ("taille: %d\n", nmatch);
pmatch = malloc (sizeof (*pmatch) * nmatch);
if (!pmatch)
{
fprintf (stderr, "Memoire insuffisante\n");
exit (1);
}
buffer = (char *) str;
match = regexec (&preg, buffer, nmatch, pmatch, 0);
while (match == 0)
{
char *str_found = NULL;
size_t size ;
int start, end;
start = pmatch[0].rm_so;
end = pmatch[0].rm_eo;
size = end - start;
str_found = malloc (sizeof (*str_found) * (size + 1));
if (str_found)
{
strncpy (str_found, &buffer[start], size);
str_found[size] = '\0';
printf ("%s\n", str_found);
free (str_found);
}
match = regexec (&preg, (buffer += end), nmatch, pmatch, 0);
}
regfree (&preg);
free (pmatch);
}
return 0;
}