我已经很久没有用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;
最佳答案
下面是一个我制作的快速示例程序来解释:
#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
基本思想是用空格分隔字符串,然后在块中查找
=
字符。如果其中一个出现,则打印该块的所需部分。