[英]Breaking the string into parts in C
Its been long since I wrote a program in C language I have a string like the one below 自从我用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"
and I need to get the ones before "=" ie VRUWFB02, VRUWFB01, assa, massmedua, masspedia. 我需要在“=”之前得到那些,即VRUWFB02,VRUWFB01,assa,massmedua,masspedia。
I am able to break the string but am not able to extract those specific words. 我能够打破字符串,但无法提取那些特定的单词。
Can any one help me with this 谁能帮我这个
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;
Here's a quick example program I made up to explain things: 这是我用来解释事情的快速示例程序:
#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;
}
And output: 并输出:
$ ./example
VRUWFB02
VRUWFB01
assa
massmedua
masspedia
The basic idea is to split the string by spaces, then look for =
characters in the chunks. 基本思想是用空格分割字符串,然后在块中查找
=
字符。 If one shows up, print the desired part of that chunk. 如果显示,则打印该块的所需部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.