#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include <rl.h>

int main(void)
{
   const char *err = NULL;
   int erroffs;
   int flags = 0;
   pcre *reg;
   char teststr[82], *p;
   char regexpr[82] = "";
   int r;

   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);

       if (! (reg = pcre_compile(regexpr, flags, &err, &erroffs, NULL))) {
           fprintf(stderr, "%s\n", err);
           continue;
       }

       if(! (r = pcre_exec(reg, NULL, teststr, strlen(teststr), 0, 0, NULL, 0))) {
           printf("Matches!!\n");
       } else {
           if (r == PCRE_ERROR_NOMATCH)
               fprintf(stderr, "No match\n");
           else
               fprintf(stderr, "Matching error %d\n", r);
       }

       pcre_free(reg);
   }

   return 0;
}