txt文本中的内容是这样的:
……
... "aa",//xx,
"bb",//Ifff,
"cc",//ITEM_I,
"dd",//ITEM_I,
"ff",//ITEM_I,
"ee",//IT,
……
现在要将双引号中的内容提取出来,转换成下面格式:
……
<id_1 text="aa" />
<id_2 text="bb" />
……
请问怎么改?
……
... "aa",//xx,
"bb",//Ifff,
"cc",//ITEM_I,
"dd",//ITEM_I,
"ff",//ITEM_I,
"ee",//IT,
……
现在要将双引号中的内容提取出来,转换成下面格式:
……
<id_1 text="aa" />
<id_2 text="bb" />
……
请问怎么改?
file.Open(_T("F:\\11.txt"),CFile::modeReadWrite); //读取文件
CString strLine(_T("")); // 一行文本
LPCTSTR szGap(_T("\"")); //以\作为判断条件
CString strText(_T("")); CString strTmp(_T("")); // “”左边的部分 CString strInsert(_T("")); //代替N的
int flag = -1; int curPos = 0;
while(file.ReadString(strLine)) //读行
{
int start = 0;
curPos = strLine.Find(szGap,start) ;
while (curPos != -1)
{
int n = 1; //ID号
strTmp = strLine.Left(curPos); //双引号左边部分
strLine.Replace(strTmp,L"<id_"/*+n+" text=")*/); //替换文本
strLine.Insert(curPos+1,' ');
if ((flag = strTmp.ReverseFind(' ') != -1))
{
strInsert = strTmp.Mid(flag +1);
strLine.Insert(curPos+1,n); //插入N序号
}
strLine.Insert(curPos+1,' ');
if((flag = strTmp.ReverseFind(' ') != -1))
{
strInsert = strTmp.Mid(flag + 1);
strLine.Insert(curPos+1,L"text="); //N序号后面的字符
}
++n;
}
start = curPos+1; //跳过第一个双引号
int m = strLine.Find(szGap,start); //计算双引号中的个数
curPos += m+2; //赋给当前位置
strTmp = strLine.Right(curPos); //获得双引号右边部分
strLine.Replace(strTmp,L" />"); //替换文本
strText += strLine+_T("\r\n"); //换行
}
file.SetLength(0); //清空文件
file.WriteString(strText); //重新写入
file.Close(); //关闭
MessageBox(_T("转换成功!"));
http://www.regexlab.com/zh/deelx/download.htm
AHK脚本如下:Loop
{
;map="\w+"
FileReadLine, line, file.txt, %A_Index%
if ErrorLevel
break
;RegExMatch(line ,map , sub)
RegExMatch(line ,"""\w+""" , sub)
FileAppend ,<id_%A_Index% text=%sub% />`n ,newfile.txt
}
MsgBox, The end of the file has been reached or there was a problem.
return
//=================================================================
#include <stdio.h>
#include <string.h>
#include <iostream.h> void main()
{
char string[] = ""
"... \"aa\",//xx,"
"\"bb\",//Ifff, "
"\"cc\",//ITEM_I, "
"\"dd\",//ITEM_I, "
"\"ff\",//ITEM_I,"
"\"ee\",//IT, "
" ……"; char seps[] = "\"";
char *token;
bool flag = false;
char buf[256] = {0};
char *p = buf;
int num=0;
printf( "%s\n\nTokens:\n", string );
token = strtok( string, seps );
while( token != NULL )
{
if (flag==true)
{
printf( "%s\n", token );
sprintf(p+num,"<id_1 text=\"%s\" />\n", token);
num += strlen(token) + strlen("<id_1 text=\"") + strlen("\" />\n");
}
flag = !flag;
token = strtok( NULL, seps );
} cout<< buf <<endl;
}
.
输出:
... "aa",//xx,"bb",//Ifff, "cc",//ITEM_I, "dd",//ITEM_I, "ff",//ITEM_I,"ee",//IT
, ……Tokens:
aa
bb
cc
dd
ff
ee
<id_1 text="aa" />
<id_1 text="bb" />
<id_1 text="cc" />
<id_1 text="dd" />
<id_1 text="ff" />
<id_1 text="ee" />Press any key to continue
再经过自己思考, 调试之后,已经成功实现功能。
改代码用C写的很简洁,赞一个,结贴了。