0

自从我用 C 语言编写程序以来已经很久了,我有一个如下所示的字符串

"VRUWFB02=I.V.R, W.F.B, plan 2, VRUWFB01=I.V.R, W.F.B, plan 1, assa=784617896.9649164, plan24, massmedua=plan12, masspedia=IVR, masojh, jhsfdkl, oijhojomn, oiafofvj, plan"

我需要得到“=”之前的那些,即VRUWFB02,VRUWFB01,assa,massmedua,masspedia。

我能够断开字符串,但无法提取那些特定的单词。

谁能帮我这个

char st[] = "VRUWFB02=I.V.R, W.F.B, plan 2, VRUWFB01=I.V.R, W.F.B, plan 1,assa=784617896.9649164, plan24, massmedua=plan12, masspedia=IVR, masojh, jhsfdkl, oijhojomn, oiafofvj, plan";
char *ch;
regex_t compiled;
char pattern[80] = "  ";
printf("Split \"%s\"\n", st);
ch = strtok(st, " ");
while (ch != NULL) {
    if(regcomp(&compiled, pattern, REG_NOSUB) == 0) {
        printf("%s\n", ch);
    }
    ch = strtok(NULL, " ,");
}
return 0;
4

2 回答 2

2

这是我为解释事情而编写的一个快速示例程序:

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

int main(void)
{
    char s[] = "VRUWFB02=I.V.R, W.F.B, plan 2, VRUWFB01=I.V.R, W.F.B, "
               "plan 1, assa=784617896.9649164, plan24, massmedua=plan12, "
               "masspedia=IVR, masojh, jhsfdkl, oijhojomn, oiafofvj, plan";
    char *p;
    char *q;

    p = strtok(s, " ");
    while (p)
    {
        q = strchr(p, '=');
        if (q)
            printf("%.*s\n", (int)(q - p), p);
        p = strtok(NULL, " ");
    }

    return 0;
}

并输出:

$ ./example
VRUWFB02
VRUWFB01
assa
massmedua
masspedia

基本思想是用空格分割字符串,然后=在块中查找字符。如果出现,打印该块的所需部分。

于 2013-07-25T20:36:48.700 回答
0

您可以使用strtok函数来断开字符串。您可以在我参考的网页上找到使用它的示例。

于 2013-07-25T20:27:53.480 回答