gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:42:06 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com