开发者

How to delete everything inside <i></i> and () out of a string in C?

开发者 https://www.devze.com 2023-03-15 20:02 出处:网络
Im sure this is easily done with regex, just haven\'t had much experience. EG, given char *mystring= blah blah <开发者_开发问答;i>(this is not needed)</i> (Nor this). This is.

Im sure this is easily done with regex, just haven't had much experience.

EG, given

char *mystring= blah blah <开发者_开发问答;i>(this is not needed)</i> (Nor this). This is.

it would return

char *return_str = blah blah  . This is.


Even though your question is tagged regex, a regex is not the correct solution.

What you probably want to do is write a simple pushdown automaton.

Here is a really simple example:

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

I'm not even sure that this qualifies as a pushdown automaton because it uses a simple counter and not a stack, but the concept is similar.

This one only does the parentheses, but it should be simple enough to demonstrate the technique.

0

精彩评论

暂无评论...
验证码 换一张
取 消