当前位置: 技术问答>linux和unix
erlang 字符串处理
来源: 互联网 发布时间:2017-05-22
本文导语: 获取的字符串如下: {ok,"nRegistrations:n=================================================================================================nCall-ID: t1282389322@192.168.1.169nUser: t1000@192.168.1.63nContact: t"1000" nAgent: tYealink SIP-T2...
获取的字符串如下:
{ok,"nRegistrations:n=================================================================================================nCall-ID: t1282389322@192.168.1.169nUser: t1000@192.168.1.63nContact: t"1000" nAgent: tYealink SIP-T20P 9.61.0.70nStatus: tRegistered(TCP-NAT)(unknown) EXP(2014-06-23 10:32:57) EXPSECS(91)nHost: tc8dev.mycomm.cnnIP: t192.168.1.169nPort: t1570nAuth-User: t1000nAuth-Realm: t192.168.1.63nMWI-Account:t1000@192.168.1.63nnCall-ID: t1498574971-5060-1@BJC.BGI.B.BFCnUser: t6001@192.168.1.63nContact: t"" nAgent: tGrandstream GXP1400 1.0.4.13nStatus: tRegistered(UDP)(unknown) EXP(2014-06-23 10:33:18) EXPSECS(112)nHost: tc8dev.mycomm.cnnIP: t192.168.1.152nPort: t5060nAuth-User: t6001nAuth-Realm: t192.168.1.63nMWI-Account:t6001@192.168.1.63nnTotal items returned: 6n=================================================================================================n"}
格字段及其值一般格式如下:
Registrations:
=================================================================================================
Call-ID: 1282389322@192.168.1.169
User: 1000@192.168.1.63
Contact: "1000"
Agent: Yealink SIP-T20P 9.61.0.70
Status: Registered(TCP-NAT)(unknown) EXP(2014-06-23 10:24:10) EXPSECS(94)
Host: c8dev.mycomm.cn
IP: 192.168.1.169
Port: 1570
Auth-User: 1000
Auth-Realm: 192.168.1.63
MWI-Account: 1000@192.168.1.63
Call-ID: 1498574971-5060-1@BJC.BGI.B.BFC
User: 6001@192.168.1.63
Contact: ""
Agent: Grandstream GXP1400 1.0.4.13
Status: Registered(UDP)(unknown) EXP(2014-06-23 10:24:16) EXPSECS(100)
Host: c8dev.mycomm.cn
IP: 192.168.1.152
Port: 5060
Auth-User: 6001
Auth-Realm: 192.168.1.63
MWI-Account: 6001@192.168.1.63
Total items returned: 2
=================================================================================================
希望将获取的字符串每一部分的值存储于列表中格式如这样的:[ {,,...}, {.....} ],求漂亮的简洁的代码,我写的太繁琐了,erlang大神请指教
{ok,"nRegistrations:n=================================================================================================nCall-ID: t1282389322@192.168.1.169nUser: t1000@192.168.1.63nContact: t"1000" nAgent: tYealink SIP-T20P 9.61.0.70nStatus: tRegistered(TCP-NAT)(unknown) EXP(2014-06-23 10:32:57) EXPSECS(91)nHost: tc8dev.mycomm.cnnIP: t192.168.1.169nPort: t1570nAuth-User: t1000nAuth-Realm: t192.168.1.63nMWI-Account:t1000@192.168.1.63nnCall-ID: t1498574971-5060-1@BJC.BGI.B.BFCnUser: t6001@192.168.1.63nContact: t"" nAgent: tGrandstream GXP1400 1.0.4.13nStatus: tRegistered(UDP)(unknown) EXP(2014-06-23 10:33:18) EXPSECS(112)nHost: tc8dev.mycomm.cnnIP: t192.168.1.152nPort: t5060nAuth-User: t6001nAuth-Realm: t192.168.1.63nMWI-Account:t6001@192.168.1.63nnTotal items returned: 6n=================================================================================================n"}
格字段及其值一般格式如下:
Registrations:
=================================================================================================
Call-ID: 1282389322@192.168.1.169
User: 1000@192.168.1.63
Contact: "1000"
Agent: Yealink SIP-T20P 9.61.0.70
Status: Registered(TCP-NAT)(unknown) EXP(2014-06-23 10:24:10) EXPSECS(94)
Host: c8dev.mycomm.cn
IP: 192.168.1.169
Port: 1570
Auth-User: 1000
Auth-Realm: 192.168.1.63
MWI-Account: 1000@192.168.1.63
Call-ID: 1498574971-5060-1@BJC.BGI.B.BFC
User: 6001@192.168.1.63
Contact: ""
Agent: Grandstream GXP1400 1.0.4.13
Status: Registered(UDP)(unknown) EXP(2014-06-23 10:24:16) EXPSECS(100)
Host: c8dev.mycomm.cn
IP: 192.168.1.152
Port: 5060
Auth-User: 6001
Auth-Realm: 192.168.1.63
MWI-Account: 6001@192.168.1.63
Total items returned: 2
=================================================================================================
希望将获取的字符串每一部分的值存储于列表中格式如这样的:[ {,,...}, {.....} ],求漂亮的简洁的代码,我写的太繁琐了,erlang大神请指教
|
使用strstr和sscanf函数写了个,
能力有限,参考下吧
能力有限,参考下吧
/*
*
*/
#include
#include
char src[2000]="nRegistrations:n=================================================================================================nCall-ID: t1282389322@192.168.1.169nUser: t1000@192.168.1.63nContact: t"1000" nAgent: tYealink SIP-T20P 9.61.0.70nStatus: tRegistered(TCP-NAT)(unknown) EXP(2014-06-23 10:32:57) EXPSECS(91)nHost: tc8dev.mycomm.cnnIP: t192.168.1.169nPort: t1570nAuth-User: t1000nAuth-Realm: t192.168.1.63nMWI-Account:t1000@192.168.1.63nnCall-ID: t1498574971-5060-1@BJC.BGI.B.BFCnUser: t6001@192.168.1.63nContact: t"" nAgent: tGrandstream GXP1400 1.0.4.13nStatus: tRegistered(UDP)(unknown) EXP(2014-06-23 10:33:18) EXPSECS(112)nHost: tc8dev.mycomm.cnnIP: t192.168.1.152nPort: t5060nAuth-User: t6001nAuth-Realm: t192.168.1.63nMWI-Account:t6001@192.168.1.63nnTotal items returned: 6n=================================================================================================n";
typedef struct{
char callId[32];
char user[32];
char contact[128];
char agent[32];
char status[128];
char host[32];
char ip[32];
char port[8];
char authUser[8];
char authRealm[32];
char mwlAccount[32];
}info;
int main()
{
int flag=0;
char dst[2000]={0};
char *s=src;
info data;
printf("%s", s);
sprintf(dst,"%s","[");
s = strstr(s,"Call-ID");
while(s && (11 == sscanf(s,
"Call-ID: t%31[^n]n"
"User: t%31[^n]n"
"Contact: t%127[^n]n"
"Agent: t%31[^n]n"
"Status: t%128[^n]n"
"Host: t%31[^n]n"
"IP: t%31[^n]n"
"Port: t%7[^n]n"
"Auth-User: t%7[^n]n"
"Auth-Realm: t%31[^n]n"
"MWI-Account:t%31[^n]n",
data.callId,
data.user,
data.contact,
data.agent,
data.status,
data.host,
data.ip,
data.port,
data.authUser,
data.authRealm,
data.mwlAccount))){
if(flag == 1){
sprintf(dst,"%s,",dst);
}else{
flag=1;
}
sprintf(dst,
"%s{,,,,,,,,,,}",
dst,data.callId,data.user,data.contact,data.agent,data.status,data.host,data.ip,data.port,data.authUser,data.authRealm,data.mwlAccount);
printf("%sn",dst);
s = s + strlen("Call-ID:");
s = strstr(s,"Call-ID");
}
sprintf(dst,"%s]",dst);
return 0;
}
|
额,erlang……
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。