Angus Leeming <[EMAIL PROTECTED]> writes: | Lars Gullik Bjønnes <[EMAIL PROTECTED]> writes: | > | If this is meant to be code to read a comma-separated list, then it's not | > | robust. This file can be edited by hand... | > | > If you edit it by hand you are on your own. | > But sure if we can make it more robust. | | > | Bo, have a look at Boost.Tokenizer. It's ideally suited to, well, | > | tokenizing | > Perhaps boost::regex will be even easier. | | I don't think so. | | #include <iostream> | #include <boost/tokenizer.hpp> | #include <string> | | int main() | { | std::string const data = "This is, a test"; | typedef boost::tokenizer<boost::char_separator<char> > Tokenizer; | // Tokenize on spaces and commas. | // Discard empty tokens. | boost::char_separator<char> sep(" ,"); | | Tokenizer tokens(data, sep); | Tokenizer::iterator const end = tokens.end(); | for (Tokenizer::iterator it = tokens.begin(); it != end; ++it) | std::cout << "<" << *tok_iter << "> "; | std::cout << "\n"; | return EXIT_SUCCESS; | }
I am still not sure. #include <iostream> #include <boost/regex.hpp> #include <string> int main() { boost::regex reg("([^,]+),[ ]+(.*)"); std::string const data = "This is, a test"; boost::smatch m; if (boost::regex_search(data, m, reg)) { std::cout << "<" << m[1] << "> "; std::cout << "<" << m[2] << "> "; std::cout << "\n"; return EXIT_SUCCESS; } return EXIT_FAILURE; } -- Lgb