提取两个定界符之间的数据 [英] Extract data between two delimiters
本文介绍了提取两个定界符之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想知道是否有可能拾取两个定界符(定界符为字符串)之间的数据。
I just want to know whether it is possible to pick up the data that is present between two delimiters (delimiter being a string).
例如原始字符串如下
<message%20type%3D"info"%20code%3D"20005">%20<text>Conference%20successfully%20modified</text>%20<data>0117246</data>%20%20</message>%20
,我希望< text>
标记之间存在数据。我需要数据的字符串可以不同。字符串也可以像这样
and I want the data that is present between <text>
tags. The string from which i need the data can be different. The string can also be like this
<message%20type%3D"info"%20code%3D"20001">%20<text>Conference%20deleted</text%20%20<vanity>0116976</vanity>%20</message>%20<message%20type%3D"info"%20code%3D"20002">%20<text>Number%20of%20conferences%20deleted</text>%20<data>1</data>%20%20</message>%20
但我总是需要< text>
标记之间的数据。
but I always need the data present between the <text>
tags.
那么用C语言有可能还是有其他选择?
So is it possible in C language or is there any alternative?
推荐答案
我会选择 strstr()
。
例如:
#include <stdio.h>
#include <string.h>
int main(void) {
char data[] = "<message%20type%3D\"info\"%20code"
"%3D\"20005\">%20<text>Conference%"
"20successfully%20modified</text>%"
"20<data>0117246</data>%20%20</mes"
"sage>%20";
char *p1, *p2;
p1 = strstr(data, "<text>");
if (p1) {
p2 = strstr(p1, "</text>");
if (p2) printf("%.*s\n", p2 - p1 - 6, p1 + 6);
}
return 0;
}
这篇关于提取两个定界符之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文