C正则表达式搜索和存储多个组
我试图在这里使用正则表达式和c来搜索具有未知组数的给定模式。到目前为止,我得到的是: 这是一种破坏性的搜索(类似于strtok)。我想让它做的是用一个字符串,去掉正则表达式模式中给定的组 例如,日期为yyyymmdd (C正则表达式搜索和存储多个组,c,regex,C,Regex,我试图在这里使用正则表达式和c来搜索具有未知组数的给定模式。到目前为止,我得到的是: 这是一种破坏性的搜索(类似于strtok)。我想让它做的是用一个字符串,去掉正则表达式模式中给定的组 例如,日期为yyyymmdd ([:digit:]{4})([:digit:]{2})([[:digit:]{2}) 如果它在字符串中找到了该模式,则提取出这些片段,并用一些可识别的字符(可能是\0)将它们分开,然后将它们存储到缓冲区中。 完成此操作并提取所有组后,用缓冲区的内容替换原始字符串。最后释放缓冲区占
[: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;
}
如果您真正的任务仅限于查找和分组一个八位数的字符串,那么regex是严重的过度使用(氢弹/蚁丘规模)。这只是我想用它做的一个例子。我有一个icalendar文件,我想把它通读一遍,然后抓取一些不同的部分。
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;
}