C Regular Expressions: Extracting the Actual Matches

前端 未结 2 1830
不思量自难忘°
不思量自难忘° 2020-12-15 13:30

I am using regular expressions in C (using the \"regex.h\" library). After setting up the standard calls (and checks) for regcomp(...) and regexec(...), I can only manage t

2条回答
  •  猫巷女王i
    2020-12-15 13:39

    Since g++ regex is bugged until who knows when, you can use my code instead (License: AGPL, no warranty, your own risk, ...)

    /**
     * regexp (License: AGPL3 or higher)
     * @param re extended POSIX regular expression
     * @param nmatch maximum number of matches
     * @param str string to match
     * @return An array of char pointers. You have to free() the first element (string storage). the second element is the string matching the full regex, then come the submatches.
    */
    char **regexp(char *re, int nmatch, char *str) {
      char **result;
      char *string;
      regex_t regex;
      regmatch_t *match;
      int i;
    
      match=malloc(nmatch*sizeof(*match));
      if (!result) {
        fprintf(stderr, "Out of memory !");
        return NULL;
      }
    
      if (regcomp(®ex, re, REG_EXTENDED)!=0) {
        fprintf(stderr, "Failed to compile regex '%s'\n", re);
        return NULL;
      }
    
      string=strdup(str);
      if (regexec(®ex,string,nmatch,match,0)) {
    #ifdef DEBUG
        fprintf(stderr, "String '%s' does not match regex '%s'\n",str,re);
    #endif
        free(string);
        return NULL;
      }
    
      result=malloc(sizeof(*result));
      if (!result) {
        fprintf(stderr, "Out of memory !");
        free(string);
        return NULL;
      }
    
      for (i=0; i=0) {
          string[match[i].rm_eo]=0;
          ((char**)result)[i]=string+match[i].rm_so;
    #ifdef DEBUG
          printf("%s\n",string+match[i].rm_so);
    #endif                                                                                                                                                                                                                                                   
        } else {                             
          ((char**)result)[i]="";            
        }
      }
    
      result[0]=string;                      
    
      return result;                         
    
    }
    

提交回复
热议问题