C 从行中删除第一个n字

C 从行中删除第一个n字,c,regex,C,Regex,这一行: anyWord = this is a placeholder to the end of line 我想删除anyWord=并获取行的其余部分。如果您确定只希望使用正则表达式来解决此问题: (?:^\w+=)(.*)$ 然后获取捕获组(组1)。在不使用正则表达式的情况下,我们可以使用strtok获取给定行中=字符的右侧: #include <ctype.h> #include <stdio.h> #include <stdlib.h> #inc

这一行:

anyWord = this is a placeholder to the end of line 

我想删除
anyWord=
并获取行的其余部分。

如果您确定只希望使用正则表达式来解决此问题:

(?:^\w+=)(.*)$


然后获取捕获组(组1)。

在不使用正则表达式的情况下,我们可以使用strtok获取给定行中
=
字符的右侧:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* getRightSideOfEquals(const char* line){

    char* linecopy = strdup(line);
    const char* result = NULL;
    char* left = strtok(linecopy, "=");
    const char* right = strtok(NULL, "=");

    while (right && isspace(*right)) {  // remove whitespace
        right++;
    }

    result = strdup(right ? right : "");
    free((void*)linecopy);
    return result;
}


int main()
{
    const char* line = "anyWord = this is a placeholder to the end of line";
    const char* right = getRightSideOfEquals(line);
    printf("%s\n", right);
    free((void*)right);
    return 0;
}
#包括
#包括
#包括
#包括
常量字符*getRightSideOfEquals(常量字符*行){
char*linecopy=strdup(行);
const char*result=NULL;
char*left=strtok(linecopy,“=”);
const char*right=strtok(NULL,“=”);
while(right&&isspace(*right)){//删除空白
右++;
}
结果=标准值(右?右:);
免费((作废*)行副本);
返回结果;
}
int main()
{
const char*line=“anyWord=这是行尾的占位符”;
const char*right=getRightSideOfEquals(行);
printf(“%s\n”,右);
自由((无效*)权利);
返回0;
}

上面的程序将打印:
这是一个占位符,位于行尾

Name=
看起来不像一个词这似乎更容易通过使用任何语言的内置字符串函数来解决。例如,在
=
上拆分并取第二部分。我使用C语言谢谢。太接近了。但我不是说“名字”,我是说“字+空格+等号+空格”。我刚刚更新了question@smalinux谢谢你抽出时间。但这对我来说仍然不起作用,我不知道为什么。检查这个:我关闭了,输出正常,我不喜欢用
charx[1]
跳过这个。。。