问题
I try to split the following text:
std::string text="1,2,3,max(4,5,6,7),array[8,9],10,page{11,12},13";
I have the following code:
std::vector<std::string> found_list;
boost::split(found_list,text,boost::is_any_of(","))
But my desired output is:
1
2
3
max(4,5,6,7)
array[8,9]
10
page{11,12}
13
Regarding parentheses and braces, how to implement it?
回答1:
You want to parse a grammar.
Since you tagged with boost let me show you using Boost Spirit:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
std::string const text="1,2,3,max(4,5,6,7),array[8,9],10,page{11,12},13";
std::vector<std::string> split;
if (qi::parse(text.begin(), text.end(),
qi::raw [
qi::int_ | +qi::alnum >> (
'(' >> *~qi::char_(')') >> ')'
| '[' >> *~qi::char_(']') >> ']'
| '{' >> *~qi::char_('}') >> '}'
)
] % ',',
split))
{
for (auto& item : split)
std::cout << item << "\n";
}
}
Prints
1
2
3
max(4,5,6,7)
array[8,9]
10
page{11,12}
13
来源:https://stackoverflow.com/questions/32792425/boost-split-not-traversing-inside-of-parenthesis-or-braces