static int gmatch_exec (TUserdata *ud, TArgExec *argE) { if (argE->startoffset > 0) argE->eflags |= REG_NOTBOL; argE->text += argE->startoffset; return tre_regnexec (&ud->r, argE->text, argE->textlen - argE->startoffset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
int main(int argc, char **argv) { regex_t preg; regmatch_t *pmatch = NULL; int i, subject_len, match_len, sub_len, errcode, ret = 1, match_count, done; char errbuf[256], *subject, *regex, *match_start, *sub; if( argc < 3 ) { printf("Usage: %s [subject] [regex]\n", argv[0]); return 0; } subject = strdup(argv[1]); subject_len = strlen(subject); regex = strdup(argv[2]); /* compile the regex */ errcode = tre_regcomp(&preg, regex, REG_EXTENDED); if(errcode) { tre_regerror(errcode, &preg, errbuf, sizeof(errbuf)); fprintf(stderr, "Error: regcomp() %s\n", errbuf); goto err; } /* allocate space for match and submatches */ pmatch = (regmatch_t*)calloc(sizeof(regmatch_t), preg.re_nsub+1); if(pmatch == NULL) { perror("Error calloc()"); goto err; } /* find all matches */ sub = subject; sub_len = subject_len; match_count = 0; done = 0; while(!done) { /* execute regex */ errcode = tre_regnexec(&preg, sub, sub_len, preg.re_nsub+1, pmatch, 0); switch(errcode) { case REG_OK: match_count++; /* loop through subpattern matches */ for(i=0; i < preg.re_nsub+1; i++) { match_len = pmatch[i].rm_eo - pmatch[i].rm_so; match_start = sub + pmatch[i].rm_so; if(i == 0) printf("Match [%d] %.*s\n", match_count, match_len, match_start); else printf(":: group [%d] %.*s\n", i, match_len, match_start); } /* set up for next regexec call */ sub_len -= pmatch[0].rm_eo; assert(sub_len >= 0); /* entire subject consumed */ if(sub_len == 0) { done = 1; break; } /* start next match were we left off */ sub += pmatch[0].rm_eo; break; case REG_NOMATCH: if(match_count == 0) puts("No matches found."); done = 1; break; case REG_ESPACE: fprintf(stderr,"Error: Out of memory.\n"); goto err; default: /* should not be here, abort */ assert(0); } } /* success */ ret = 0; err: /* cleanup */ if(regex) free(regex); if(subject) free(subject); if(&preg) tre_regfree(&preg); if(pmatch) free(pmatch); return ret; }
int tre_regexec(const regex_t *preg, const char *str, size_t nmatch, regmatch_t pmatch[], int eflags) { return tre_regnexec(preg, str, (unsigned)-1, nmatch, pmatch, eflags); }
static int split_exec (TPosix *ud, TArgExec *argE, int offset) { if (offset > 0) argE->eflags |= REG_NOTBOL; return tre_regnexec (&ud->r, argE->text + offset, argE->textlen - offset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int gsub_exec (TPosix *ud, TArgExec *argE, int st) { if (st > 0) argE->eflags |= REG_NOTBOL; return tre_regnexec (&ud->r, argE->text+st, argE->textlen-st, ALG_NSUB(ud)+1, ud->match, argE->eflags); }
static int findmatch_exec (TPosix *ud, TArgExec *argE) { argE->text += argE->startoffset; return tre_regnexec (&ud->r, argE->text, argE->textlen - argE->startoffset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
int crm114__regnexec(const regex_t *preg, const char *string, long string_len, size_t nmatch, regmatch_t pmatch[], int eflags) { return tre_regnexec(preg, string, string_len, nmatch, pmatch, eflags); }