#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include <rl.h>
#define REGEXEC
int main(void)
{
regex_t reg;
regmatch_t pmatch[1];
char rbuf[256];
char teststr[82], *p;
char regexpr[82] = "";
int r;
#ifndef REGEXEC
char *rs;
#endif
if (! (p = rl_gets("Enter a test string: ", NULL)))
exit(0);
strcpy(teststr, p);
while(1) {
if (! (p = rl_gets("Enter a regular expression: ", regexpr)))
exit(0);
if(! strlen(p)) {
if (! (p = rl_gets("Enter a test string: ", NULL)))
exit(0);
if(! strlen(p))
break;
strcpy(teststr, p);
continue;
}
strcpy(regexpr, p);
#ifdef REGEXEC
if((r = regcomp(®, regexpr, REG_EXTENDED))) {
fprintf(stderr, "%d\n", r);
regerror(r, ®, rbuf, 256);
printf("%s\n", rbuf);
regfree(®);
continue;
}
if(! (r = regexec(®, teststr, 1, pmatch, 0))) {
printf("Matches!!\n");
printf("Offset: %d\n", (int)pmatch[0].rm_so);
} else {
regerror(r, ®, rbuf, 256);
printf("%s\n", rbuf);
}
regfree(®);
#else
if((rs = re_comp(regexpr)) != NULL) {
printf("%s\n", rs);
continue;
}
if((r = re_exec(teststr)) == 1)
printf("Matches!!\n");
else
printf("No match\n");
#endif
}
return 0;
}