首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

简单的解析字符串有关问题

2012-02-21 
简单的解析字符串问题有:~qwe234~ere345~tertyt~454rter怎么解析出来:得到:qwe234ere345tertyt454

简单的解析字符串问题
有:~qwe=234~ere=345~ter=tyt~454=rter
怎么解析出来:
得到:qwe=234
            ere=345
            ter=tyt
            454=rter


~~~~~~~~~~~~
急用!!

[解决办法]
int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;

infile.open(filename.c_str());
string line;
getline(infile, line, '\n ');
infile.close();

vector <string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( '~ ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));
}
[解决办法]
for(vector <string> ::const_iterator cit = wordsOfLine.begin(); cit != wordsOfLine.end(); ++ cit)
{
cout < < *cit < < endl;
}
即可以显示出
呵呵^_^,okokok
[解决办法]
#include <string.h>
#include <stdio.h>

char string[] = "~qwe=234~ere=345~ter=tyt~454=rter;
char seps[] = "~ ";
char *token;

int main( void )
{
token = strtok( string, seps );
while( token != NULL )
{
printf( " %s\n ", token );
token = strtok( NULL, seps );
}
}
[解决办法]
木知道lz想怎么弄的说
[解决办法]
#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
char str_tmp[128],str1[128],str2[128],str3[128],str4[128];
cin> > setw(128)> > str_tmp;
char *str_ptr[4]={
str1,str2,str3,str4
};

int i=0,n=0,m=0;
while(str_tmp[i]== '~ ')
{
while((str_tmp[++i]!= '~ ')&&str_tmp[i]!= '\0 ')
{
*(str_ptr[n]+m)=str_tmp[i];
m++;
}
*(str_ptr[n]+m+1)= '\0 ';
n++;
m=0;
}

for(int n=0;n <4;n++)
cout < <str_ptr[n] < <endl;
system( "pause ");
return 0;
}

WinXP+SP2 Dev-Cpp 4.9.9.2环境下调试通过
[解决办法]
C/C++ 不是这样滴 ...

楼主你那个是啥?
PHP么?
[解决办法]
最多只能根据串解析出一些“关系”:

#include <map>
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
string line( "~qwe=234~ere=345~ter=tyt~454=rter ");
map <string, string> strmap;
map <string, string> ::iterator it;

string str, pre, suf;
string::size_type index=0;


index=line.find( '~ ', 0);
while(index != string::npos)
{
str=line.substr(index+1, line.find( '~ ', index+1)-index-1);
pre=str.substr(0, line.find( '= ', index)-index-1);
suf=str.substr(pre.length()+1);

strmap.insert(make_pair(pre, suf));
index=line.find( '~ ', index+1);
}

for(it=strmap.begin(); it!=strmap.end(); it++)
cout < <it-> first < < " = " < <it-> second < <endl;

system( "pause ");
return 0;
}

输出:
qwe = 234
ere =3 45
ter = tyt
454 = rter
[解决办法]
解析出来的数据已经保存在 strmap 中了,
如果有其他的用途,
可以进行后续操作。
[解决办法]
此问题最直接的解法是如楼上几位的手工分析处理.另一个思路是使用Boost库处理字符串(有点杀鸡用牛刀的感觉,呵呵).
Boost中至少有三个库可解决此题: Tokenizer,regex和spirit.
我们的目的是实现函数:
void Split(const string& str, vector <string> & result);
str是待处理的字符串,如 "~qwe=234~ere=345~ter=tyt~454=rter "
result中将存放处理结果, 如{ "qwe=234 ", "ere=345 ", "ter=tyt ", "454=rter "}
三种方法分别如下:

============================= tokenizer
#include <iostream>
#include <string>
#include <vector>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;

void Split(const string& str, vector <string> & result)
{
typedef tokenizer <char_separator <char> > MyTokenizer;
char_separator <char> sep( "~ ");

MyTokenizer tok(str,sep);
result.clear();
for(MyTokenizer::iterator beg=tok.begin(); beg!=tok.end();++beg){
result.push_back(*beg);
}
}

============================== regex
#include <iostream>
#include <string>
#include <vector>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;

void Split(const string& str, vector <string> & result)
{
std::string::const_iterator start, end;
start = str.begin();
end = str.end();
smatch m;
static const regex expression( "~([^\~]*) ");
int now = 0;
while(regex_search(start, end, m, expression))
{
string s = m[1];
result.push_back(s);
start = m[0].second;
}
}

============================== spirit
#include <iostream>
#include <string>
#include <vector>
#include <boost/spirit/core.hpp>
#include <boost/spirit/phoenix/primitives.hpp>
#include <boost/spirit/phoenix/operators.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost;
using namespace boost::spirit;
using namespace phoenix;

void add_str(string::const_iterator begin, string::const_iterator end, vector <string> & result)
{
int n = 2;
string s(begin,end);
result.push_back(s);
}

void Split(const string& str, vector <string> & result)
{
parse(str.begin(), str.end(),
(
* ( '~ ' > > (* ~ch_p( '~ '))[bind(&add_str, _1, _2, ref(result))])
)
,
space_p);
}

==================================
下面是用于测试的代码:
int main()
{
vector <string> res;


Split( "~qwe=234~ere=345~ter=tyt~454=rter ", res);
for (int i=0; i <res.size(); i++) {
cout < < res[i] < < endl;
}

return 0;
}

热点排行