Boostの正規表現ライブラリを使用して、ラベルと数字を含む文字列をトークンに分離したいと思います。たとえば、'abc1def002g30'
に分割され{'abc','1','def','002','g','30'}
ます。Boostのドキュメントに記載されている例を変更して、次のコードを作成しました。
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
int main(int argc,char **argv){
string s,str;
int count;
do{
count=0;
if(argc == 1)
{
cout << "Enter text to split (or \"quit\" to exit): ";
getline(cin, s);
if(s == "quit") break;
}
else
s = "This is a string of tokens";
boost::regex re("[0-9]+|[a-z]+");
boost::sregex_token_iterator i(s.begin(), s.end(), re, 0);
boost::sregex_token_iterator j;
while(i != j)
{
str=*i;
cout << str << endl;
count++;
i++;
}
cout << "There were " << count << " tokens found." << endl;
}while(argc == 1);
return 0;
}
保存されているトークンの数count
は正しいです。ただし、*it
空の文字列のみが含まれているため、何も出力されません。私が間違っていることについて何か推測はありますか?
編集:以下に提案されている修正に従って、コードを変更しましたが、正しく機能するようになりました。