[xxxxx][NOTEPAD.EXE|C:\WINDOWS\NOTEPAD.EXE|193536|2009/7/14 9:39:25|F2C7BB8ACC97F92E987A2D4087D021B1|6.1.7600.16385 (win7_rtm.090713-1255)|MICROSOFT CORPORATION][GHOSTEXP.EXE|D:\新建文件夹\GHOSTEXP.EXE|854408|2010/1/2 8:0:0|E0F5E0769B2E6048824BF49507546BB6|11.0.2.1573|SYMANTEC 公司][xxxx][xxxx]...[xxxx]这种的一种字符串,我怎样读取其中的一段[]之间的内容,比如[NOTEPAD.EXE|C:\WINDOWS\NOTEPAD.EXE|193536|2009/7/14 9:39:25|F2C7BB8ACC97F92E987A2D4087D021B1|6.1.7600.16385 (win7_rtm.090713-1255)|MICROSOFT CORPORATION]怎样读出来
或者std::string 的find, substring等
[]匹配就完事了
char **p1=NULL,*p2=NULL;
char *strRes[1000];//查找到的所有[],因为只是个sample,随便定了最多1000个[]组memset(strRes,0,1000*sizeof(char*));p1=strchr(sBuffer,'[');int nCount=0;
while (p1)
{
p2=strchr(p1,']'); if(p2==NULL)
{
break;
} strRes[nCount]=new char[p2-p1+2];
strncpy(strRes[nCount],p1,p2-p1+1);
p1=strchr(p2,'['); nCount++;
}
if (p)
++p;
else
取不到,退出;
char* q = strchr(p, ']');
if (q)
{
str = q + 1;
*q = '\0';
}
else
取不到,退出;
现在p指向的就是第一段,str指向第二个'[',循环取下,自己判断判断str结束了没就行了
int nPos = 2; // 匹配第n个 [] 里的内容
char *pStart;
char *pEnd = str; for (int i = 0; i < nPos; i ++)
{
if (pEnd)
{
pStart = strchr(pEnd, '[');
if (pStart)
{
pEnd = strchr(pStart, ']');
}
}
}
if (pStart && pEnd)
{
int len = pEnd - pStart;
char *mat = new char[len + 1];
strncpy(mat, pStart, len);
mat[len] = 0;
printf("%s", mat);
delete[] mat;
}
int pos1 = strText.Find("[");
int pos2 = strText.Find("]", pos1);
CString strResult = strText.Mid(pos1+1, pos2-pos1);
起始位置搞错了char *str = "[xxxxx][NOTEPAD.EXE|C:\\WINDOWS\\NOTEPAD.EXE|193536|2009/7/14 9:39:25|F2C7BB8ACC97F92E987A2D4087D021B1|6.1.7600.16385 (win7_rtm.090713-1255)|MICROSOFT CORPORATION][GHOSTEXP.EXE|D:\\新建文件夹\\GHOSTEXP.EXE|854408|2010/1/2 8:0:0|E0F5E0769B2E6048824BF49507546BB6|11.0.2.1573|SYMANTEC 公司][xxxx][xxxx]...[xxxx]";
int nPos = 2; // 匹配第n个 [] 里的内容
char *pStart;
char *pEnd = str; for (int i = 0; i < nPos; i ++)
{
if (pEnd)
{
pStart = strchr(pEnd, '[');
if (pStart)
{
pEnd = strchr(pStart, ']');
}
}
}
if (pStart && pEnd)
{
int len = pEnd - pStart - 1;
char *mat = new char[len + 1];
strncpy(mat, pStart + 1, len);
mat[len] = 0;
printf("%s", mat);
delete[] mat;
}
_T("[GHOSTEXP.EXE|D:\\新建文件夹\\GHOSTEXP.EXE|854408|2010/1/2 8:0:0|E0F5E0769B2E6048824BF49507546BB6|11.0.2.1573|SYMANTEC 公司]")
_T("[xxxx][xxxx]...[xxxx]"); TCHAR seps[] = _T("[]");
TCHAR* token = _tcstok(buf, seps);
while(NULL != token)
{
_tprintf(_T("%s\n"), token);
token = _tcstok(NULL, seps);
}
[xxxxx][GHOSTEXP.EXE|D:\新建文件夹\GHOSTEXP.EXE|854408|2010/1/28:0:0|E0F5E0769B2E6048824BF49507546BB6|11.0.2.1573|SYMANTEC 公司]
[xxxx][xxxx]...[xxxx]
是不是这样格式的,
还有这些字符串之间有没有转行符?
_tcschr和_tcspbrk都可以
boost::tokenizer<boost::char_separator<char > > tokens(sIn, sep);