Using Boost Tokenizer escaped_list_separator with different parameters

懵懂的女人 提交于 2019-11-27 22:35:26

try this:

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

int main()
{
    using namespace std;
    using namespace boost;
    string s = "exec script1 \"script argument number one\"";
    string separator1("");//dont let quoted arguments escape themselves
    string separator2(" ");//split on spaces
    string separator3("\"\'");//let it have quoted arguments

    escaped_list_separator<char> els(separator1,separator2,separator3);
    tokenizer<escaped_list_separator<char>> tok(s, els);

    for(tokenizer<escaped_list_separator<char>>::iterator beg=tok.begin(); beg!=tok.end();++beg)
    {
        cout << *beg << "\n";
    }
}

It seems like you're declaring your tokenizer type incorrectly.

typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
boost::escaped_list_separator<char> Separator( '\\', ' ', '\"' );
Tokenizer tok( s, Separator );

for( Tokenizer::iterator iter = tok.begin(); iter != tok.end(); ++iter )
{ cout << *iter << "\n"; }

You want to make a boost::tokenizer< boost::escaped_list_separator< char > > typed object with a boost::escaped_list_separator< char > separator object as its TokenizerFunc.

A relevant point yet not the answer for this is that if user want to output a double quote in the result, the embedded quote ("") described in the Wikipedia (like here) should be replaced with a string of (\\\"), where \\ means a escape char and \" means the quote mark. In this way, the quote mark will be displayed in the output result. The following code snippet is an example.

typedef boost::escaped_list_separator<char> std_token;
typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;

std_token a_token( "\\", ",", "\"" );
std::string s = "\"Boost,\\\" C++ Libraries\" ";
tokenizer tok{ s, a_token };
for ( const auto &t : tok )
    std::cout << t << '\n';

This is a typical output for this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!