C正则表达式搜索和存储多个组

时间:2013-12-02 07:07:44

标签: c regex

我正在尝试使用正则表达式和c来搜索具有未知数量的组的给定模式。到目前为止,我所拥有的是: 这是一种破坏性搜索(类似于strtok)。我想要它做的是接受一个字符串切出正则表达式模式给出的组 例如,对于yyyymmdd的日期 ([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2}) 如果它在字符串中找到该模式,则提取出这些片段并用一些可识别的字符(可能是\0)将它们分开并将它们存储到缓冲区中。 完成该操作并且已提取所有组后,将原始字符串替换为缓冲区的内容。最后释放缓冲区占用的内存。

int search(char *string, char *pattern)
{
    int status;
    regex_t re;
    size_t n_groups;
    int begin, end, length;
    char *match_text;

    if(regcomp(&re, pattern, REG_EXTENDED) != 0)
        {
            return EXIT_SUCCESS;
        }
    n_groups = re.re_nsub + 1;
    regmatch_t * groups = (regmatch_t*)malloc(n_groups * sizeof(regmatch_t));
    if (groups == NULL)
      {
        fprintf(stderr, "Error allocating regex groups\n");
        return EXIT_FAILURE;
      }
    status = regexec(&re, string, n_groups, groups, 0);
    if(status != 0)
        {
            fprintf(stderr, "No matches found\n");
            return EXIT_SUCCESS;
        }
    begin = groups[1].rm_so;
    end = groups[1].rm_eo;
    length = end - begin;
    match_text = (char*)malloc(sizeof(char) * (length + 1));
    if(match_text == NULL)
        {
            fprintf(stderr, "Error allocating %ld bytes\n", sizeof(char) * (length + 1));
            exit(EXIT_FAILURE);
        }
    strncpy(match_text, string + begin, length);
    match_text[length] = '\0';
    regfree(&re);

    /* Adjust size of string to contain matches */
    string = (char*)realloc(string, (sizeof(char) * length+1));
    if (string == NULL)
        {
            fprintf(stderr, "Error allocating %ld bytes\n", sizeof(char) * (length + 1));
            return EXIT_FAILURE;
        }
    strncpy(string, match_text, length);


    free(match_text);
    return EXIT_SUCCESS;
}

现在,它正确地找到了组的数量,并且似乎为缓冲区分配了正确的内存量。然后它替换原始字符串的内容。如何让它抓住其他组并将它们分开?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我似乎有一些有用的东西。我不知道它是否“好”,但确实有效。

char* search(char *string, char *pattern)
{
  regex_t compiled;
  regmatch_t* groups;
  size_t n_groups;
  int begin, end, length, group, total_length;
  char* match_text;
  char* new_string;

  if(regcomp(&compiled, pattern, REG_EXTENDED) != 0)
    {
      fprintf(stderr, "Could not compile regex\n");
      return string;
    }
  n_groups = compiled.re_nsub + 1;
  groups = (regmatch_t*)malloc(n_groups * sizeof(regmatch_t));
  if(groups == NULL)
    {
      fprintf(stderr, "Error allocating regex groups\n");
      return string;
    }
  if(regexec(&compiled, string, n_groups, groups,0 ) != 0)
    {
      fprintf(stderr, "No matches found\n");
      return string;
    }

  total_length = (groups[0].rm_eo - groups[0].rm_so) ;
  new_string = (char*)malloc(sizeof(char) * (total_length + 1));
  if(new_string == NULL)
    {
      fprintf(stderr, "Error allocating %ld bytes\n",sizeof(char) * (total_length + 1) );
      return NULL;
    }
 /* Clean new string*/
  new_string[0] = '\0';
  group = 1;
  begin = groups[group].rm_so;
  end = groups[group].rm_eo;
  length = end - begin;
  match_text = (char*)malloc(sizeof(char) * (length + 1));
  if(match_text == NULL)
    {
      fprintf(stderr, "Error allocating %ld bytes\n",sizeof(char) * (total_length + 1) );
      return NULL;
    }
 /* Clean new string*/
  match_text[0] = '\0';
  strncpy(match_text, string + begin, length);
  match_text[length] = '\0';
  new_string = append_string(new_string, match_text, "");
  group++;
  while (group < n_groups) {
      begin= groups[group].rm_so;
      end= groups[group].rm_eo;

      length = end - begin;
      match_text = (char*)malloc(sizeof(char) * (length + 1));
      if(match_text == NULL)
        {
          fprintf(stderr, "Error allocating %ld bytes\n",sizeof(char) * (total_length + 1) );
          return NULL;
        }
      match_text[0] = '\0';
      strncpy(match_text, string + begin, length);
      match_text[length] = '\0';
      new_string = append_string(new_string, match_text, ",");

      group++;
    }

  regfree(&compiled);
  free(match_text);
  return new_string;
}

我写了一个小辅助函数“append_string”

char* append_string(char* string, char* string2, char* sep)
{
  char* new_string;
  if((new_string = (char*)malloc(strlen(string) + strlen(string2) + strlen(sep)+ 1) ) == NULL)
    {
      fprintf(stderr, "Error appending strings" );
      return NULL;
    }
  new_string[0] = '\0';
  strcat(new_string, string);
  strcat(new_string, sep);
  strcat(new_string, string2);

  return new_string;
}