首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除行中的前n个单词

问删除行中的前n个单词
EN

Stack Overflow用户
提问于 2021-05-02 14:56:20
回答 2查看 50关注 0票数 0

这一行:

代码语言:javascript
复制
anyWord = this is a placeholder to the end of line 

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

EN

回答 2

Stack Overflow用户

发布于 2021-05-02 15:03:45

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

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

然后取捕获组(组1)。

票数 0
EN

Stack Overflow用户

发布于 2021-05-03 01:38:31

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

代码语言:javascript
复制
#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;
}

上面的程序将打印:this is a placeholder to the end of line

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67353752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档