Увеличить разделенную строку пустой строкой - PullRequest
2 голосов
/ 20 июня 2020

Есть ли способ использовать boost :: split для разделения строки при обнаружении пустой строки?

Вот отрывок того, что я имею в виду.

std::stringstream source;
source.str(input_string);
std::string line;
std::getline(source, line, '\0');
std::vector<std::string> token;
boost:split(token,line, boost::is_any_of("what goes here for blank line");

1 Ответ

2 голосов
/ 21 июня 2020

Вы можете разделить на двойной \n\n, если вы не имели в виду пустую строку как «строку, которая может содержать другие пробелы».

Live On Coliru

#include <boost/regex.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <sstream>
#include <iostream>
#include <iomanip>

int main() {
    std::stringstream source;
    source.str(R"(line one

that was an empty line, now some whitespace:
      
bye)");

    std::string line(std::istreambuf_iterator<char>(source), {});
    std::vector<std::string> tokens;

    auto re = boost::regex("\n\n");
    boost::split_regex(tokens, line, re);

    for (auto token : tokens) {
        std::cout << std::quoted(token) << "\n";
    }
}

Печатает

"line one"
"that was an empty line, now some whitespace:
      
bye"

Разрешить пробелы в «пустых» строках

Просто express в регулярном выражении:

auto re = boost::regex(R"(\n\s*\n)");

Сейчас вывод: Live On Coliru

"line one"
"that was an empty line, now some whitespace:"
"bye"
...