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

标签 c regex

我正在尝试使用 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;
}

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

提前谢谢您。

最佳答案

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

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

关于C 正则表达式搜索和存储多个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20322648/

相关文章:

python - 使用正则表达式提取复杂文本 Python

java - 多项式表达式的正则表达式

regex - 使用 RegEx 解析 SQL 文件名中的日期时间戳

c - 重新分配() : invalid next size glibc detected

iphone - 赋值从整数生成指针而不进行强制转换

c - 将结构指针传递给函数时出现错误 "ISO C forbids forward parameter decleration"?

循环链表

python - 在python中通过正则表达式提取Alberta(加拿大)邮政编码

javascript - 获取句子中的单词

c++ - 1.#INF00、-1.#IND00 和 -1.#IND 是什么意思?