Пример #1
0
char *aregx(const char *regex,const char *string) {

     regex_t p;
     regmatch_t *pmatch;
     int rerr;
     /* const char *regex = "(aab(c[0-9]*z)[ ]*.*$)|(^[^#]+$)"; */
     /* char string[BUFSIZ+1];   */
     size_t i;



     if ((rerr = regcomp(&p, regex, REG_EXTENDED | REG_NEWLINE))) {
         do_regerror(rerr, &p);
     }

     /* alloca is okay only for Linux */
     pmatch = alloca(sizeof(regmatch_t) * (p.re_nsub+1));
     if (!pmatch) {
         perror("alloca");
     }


     if ((rerr = regexec(&p, string, p.re_nsub+1, pmatch, 0))) {
         if (rerr == REG_NOMATCH) {
             /* regerror can handle this case, but in most cases
              * it is handled specially
              */
             printf("String did not match %s\n", regex);
         } else {
             do_regerror(rerr, &p);
         }
     } else {
         /* match succeeded */
         printf("String matched regular expression %s\n", regex);
         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 = (size_t) (pmatch[i].rm_eo - pmatch[i].rm_so);
                 submatch = malloc(matchlen+1);
                 strncpy(submatch, string+pmatch[i].rm_so,
                         matchlen);
                 submatch[matchlen] = '\0';
                 printf("matched subexpression %d: %s\n", i,
                        submatch);
                 free(submatch);
             } else {
                 printf("no match for subexpression %d\n", i);
             }
         }
     }
     return NULL;
}
Пример #2
0
int main() {

    regex_t p;
    regmatch_t *pmatch;
    int rcomp_err, rexec_err;
    char string[BUFSIZ+1];
    int i;

    rcomp_err = regcomp(&p, "(^(.*[^\\])#.*$)|(^[^#]+$)",
                REG_EXTENDED | REG_NEWLINE);
    if(rcomp_err) {
        do_regerror(rcomp_err, &p);
    }

    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+1, pmatch, 0);
    if(rexec_err) {
        do_regerror(rexec_err, &p);
    } 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);
            }
        }
    }
    exit(0);
}
Пример #3
0
/*=***************************************************************************/
int configuration_read(configuration * list, const char *filepath)
{
   int            errcode;
   char           temp[BUFSIZ];
   size_t         matchlen;

   FILE          *stream;
   char          *value,
                 *p;
   char          *keyword;

   regex_t        preg;
   regmatch_t    *pmatch;
   const char    *regex;

   stream = fopen(filepath, "r");
   if (stream == NULL)
   {
      return -1;
   }

   /* 
    * match: 'keyword=value'
    * and  : 'keyword = value'
    * and  : '"key word" = value'
    * and  : 'keyword = value # comment'
    * and  : 'keyword = value1 value2 # comment'
    * and  : 'keyword = #ffffff'
    * not  : '#keyword = value'
    */
   regex = "^[ \t\"']*([^#]*[^ \t\"'#])[ \t\"']*=[ \t\"']*(#?[^\"'#]*)";
   errcode = regcomp(&preg, regex, REG_EXTENDED | REG_NEWLINE);
   if (errcode)
   {
      do_regerror(errcode, &preg);
      return -1;
   }
   pmatch = (regmatch_t *) malloc(sizeof(regmatch_t) * (preg.re_nsub + 1));
   if (!pmatch)
   {
      fprintf(stderr, "out of memory\n");
      exit(8);
   }

   while (fgets(temp, sizeof(temp), stream) != NULL)
   {
      /* fgets keeps the newline - delete it */
      if ((p = strchr(temp, '\n')) != NULL)
      {
         *p = '\0';
      }
      errcode = regexec(&preg, temp, (preg.re_nsub + 1), pmatch, 0);
      if (errcode)
      {
         /* line did not match */
         continue;
      }
      /* ignore pmatch[0] which is the entire match */

      /* keyword */
      if (pmatch[1].rm_so != -1)
      {
         matchlen = pmatch[1].rm_eo - pmatch[1].rm_so;
         keyword = (char *) malloc(matchlen + 1);
         if (!keyword)
         {
            fprintf(stderr, "out of memory\n");
            exit(8);
         }
         strncpy(keyword, temp + pmatch[1].rm_so, matchlen);
         keyword[matchlen] = '\0';
      }
      else
      {
         fprintf(stderr, "missing keyword: %s\n", temp);
         continue;
      }
      /* value */
      if (pmatch[2].rm_so != -1)
      {
         matchlen = pmatch[2].rm_eo - pmatch[2].rm_so;
         value = (char *) malloc(matchlen + 1);
         if (!value)
         {
            fprintf(stderr, "out of memory\n");
            exit(8);
         }
         strncpy(value, temp + pmatch[2].rm_so, matchlen);
         value[matchlen] = '\0';
      }
      else
      {
         fprintf(stderr, "missing value: %s\n", temp);
         free(keyword);
         keyword = NULL;
         continue;
      }
      configuration_add(list, keyword, value);

      free(keyword);
      keyword = NULL;
      free(value);
      value = NULL;
   }

   free(pmatch);
   regfree(&preg);
   fclose(stream);

   return 0;
}