2017-11-25 2 views
-2

Ich bin ziemlich neue Bibliotheken zu steigern und ich versuche, die folgende Zeichenfolge mit boost::split aufzuspalten:Splitting ein String boost :: geteilt

std::string line1 = R"(1500,"Rev, H., Tintin, K.H. Ken",204400,350)"; 

Ich versuche, die oben Zeichenfolge in aufzuspalten:

{ "1500", "Rev, H., Tintin, K.H. Ken", "204400", "350"} 

Ich kann Komma , nicht als Trennzeichen für Split verwenden, da das Element in Anführungszeichen Kommas enthalten kann. Gibt es eine Möglichkeit, dass ich angeben kann, dass das Trennzeichen in Anführungszeichen ignoriert werden soll, indem man einen regulären Ausdruck verwendet?

+0

Mögliche Duplikat https://stackoverflow.com/questions/890895/using-escaped-list-separator-with-boost-split – negacao

+0

Mögliche doppelte von [Using masked \ _list \ _separator mit boost split] (https://stackoverflow.com/questions/890895/using-escaped-list-separator-with-boost-split) – mkaes

+0

Für Bildungszwecke eine einfache Regex, um sie zu teilen: [a-zA-Z0-9. "] (?: [a-zA-Z0-9." \ s] *), | \ w \ d + – jsn

Antwort

0

nur Standardbibliothek verwenden:

Live On Coliru

#include <iostream> 
#include <sstream> 
#include <iomanip> 

int main() { 
    std::istringstream line1(R"(1500,"Rev, H., Tintin, K.H. Ken",204400,350)"); 

    char ch; 
    struct { int id; 
     std::string full_title; 
     int64_t some; 
     int64_t data; 
    } record; 

    if ((line1 >> record.id) 
     && (line1 >> ch && ch == ',') 
     && (line1 >> std::quoted(record.full_title)) 
     && (line1 >> ch && ch == ',') 
     && (line1 >> record.some) 
     && (line1 >> ch && ch == ',') 
     && (line1 >> record.data)) 
    { 
     std::cout << "Parsed: \n"; 
     std::cout << " record.id = " << record.id << "\n"; 
     std::cout << " record.full_title = " << record.full_title << "\n"; 
     std::cout << " record.some = " << record.some << "\n"; 
     std::cout << " record.data = " << record.data << "\n"; 
    } 
} 

Drucke

Parsed: 
    record.id = 1500 
    record.full_title = Rev, H., Tintin, K.H. Ken 
    record.some = 204400 
    record.data = 350 
0

Boost-Geist verwenden:

Live On Coliru

#include <boost/spirit/include/qi.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

struct Record { 
    int id; 
    std::string full_title; 
    int64_t some; 
    int64_t data; 
}; 

BOOST_FUSION_ADAPT_STRUCT(Record, id, full_title, some, data) 

namespace qi = boost::spirit::qi; 

int main() { 
    using It = std::string::const_iterator; 
    qi::rule<It, std::string()> quoted = '"' >> *('\\' >> qi::char_ | ~qi::char_('"')) >> '"'; 
    qi::rule<It, Record()> parser = qi::skip(',') [qi::int_ >> quoted >> qi::int_ >> qi::int_]; 

    std::string const line1(R"(1500,"Rev, H., Tintin, K.H. Ken",204400,350)"); 

    Record record; 
    if (parse(line1.begin(), line1.end(), parser, record)) 
    { 
     std::cout << "Parsed: \n"; 
     std::cout << " record.id = " << record.id << "\n"; 
     std::cout << " record.full_title = " << record.full_title << "\n"; 
     std::cout << " record.some = " << record.some << "\n"; 
     std::cout << " record.data = " << record.data << "\n"; 
    } 
} 

Drucke

Parsed: 
    record.id = 1500 
    record.full_title = Rev, H., Tintin, K.H. Ken 
    record.some = 204400 
    record.data = 350