gpt4 book ai didi

c - 如何从 C 中的字符串中删除 和 () 内的所有内容?

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:20 24 4
gpt4 key购买 nike

我确信这很容易用正则表达式完成,只是没有太多经验。
EG,给定

char *mystring= blah blah <i>(this is not needed)</i> (Nor this). This is.

它会返回

char *return_str = blah blah  . This is.

最佳答案

即使您的问题被标记为 regexa regex is not the correct solution .

您可能想要做的是编写一个简单的 pushdown automaton .

这是一个非常简单的例子:

char* strip_parens(char* string) {
int len = strlen(string);
char* result = malloc(len + 1);
int num_parens = 0;
int i = 0, j = 0;
for(; i < len; i++) {
char c = string[i];
if(c == '(') {
num_parens++;
} else if(c == ')' && num_parens > 0) {
num_parens--;
} else if(num_parens == 0) {
result[j] = c;
j++;
}
}
result[j] = '\0';
return result;
}

我什至不确定这是否符合下推自动机的条件,因为它使用一个简单的计数器而不是堆栈,但概念是相似的。

这一个只做括号,但它应该足够简单来演示该技术。

关于c - 如何从 C 中的字符串中删除 <it></in> 和 () 内的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6510873/

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