How get all numbers by regex?
How get all numbers by regex?
If I input string like "34df4354sdf234ds23", after regexec I get only:
submatch: 1
match 0: 34
How get something like this:
submatch: 4
match 0: 34
match 0: 4354
match 0: 234
match 0: 23
???
Code:
regex_t p;
regmatch_t *pmatch;
int rcomp_err, rexec_err;
char string[BUFSIZ+1];
int i;
rcomp_err = regcomp(&p, "([0-9])*",
REG_EXTENDED | REG_NEWLINE);
pmatch = alloca(sizeof(regmatch_t) * (p.re_nsub+1));
if(!pmatch) {
perror("alloca");
}
printf("Input a string: ");
fgets(string, sizeof(string), stdin);
rexec_err = regexec(&p, string, p.re_nsub, pmatch, 0);
printf("submatch: %i\n",p.re_nsub);
if(rexec_err) {
printf("errrror");
} else {
/* match succeeded */
for(i = 0; i < p.re_nsub; i++) {
/* print the matching portion(s) of the string */
if(pmatch[i].rm_so != -1) {
char * submatch;
size_t matchlen = pmatch[i].rm_eo - pmatch[i].rm_so;
submatch = malloc(matchlen+1);
strncpy(submatch, string+pmatch[i].rm_so, matchlen);
submatch[matchlen] = '\0';
printf("match %i: %s\n", i, submatch);
free(submatch);
}
}
}