boost::split 仅当 c++ 中存在定界符时才拆分

boost::split to split only if delimiter is present in c++

我正在使用 boost::split,但如果字符串没有定界符,则它 return 是向量中的字符串。如果字符串中没有定界符,我希望它不会 return 任何东西。

#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
  
int main()
{
    string input("abcd");
    vector<string> result;
    boost::split(result, input, boost::is_any_of("\t"));
  
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << endl;
    return 0;
}

输出为abcd。如果字符串中不存在定界符,我希望向量为空。请提出建议。

看起来您可能需要一个验证解析器。正则表达式可能是一个很好的起点,但我建议使用解析器生成器,因为您很可能需要更多

My crystal ball whispers that you might be parsing command line output or CSV/TSV files

这就是您可以使用 Boost Spirit X3 做的事情:

template <typename Cont>
bool parse_columns(std::string_view input, Cont& container,
                   unsigned required = 2) {
    namespace x3 = boost::spirit::x3;

    auto valid = [required](auto& ctx) {
        x3::_pass(ctx) = x3::_val(ctx).size() >= required;
    };

    auto delim = x3::char_('\t');
    auto field = *(~delim);
    auto rule
        = x3::rule<struct _, Cont, true>{"rule"} 
        = (field % delim)[valid];

    return parse(begin(input), end(input), rule, container);
}

这是一个带测试用例的现场演示:

Live On Compiler Explorer

#include <boost/spirit/home/x3.hpp>
#include <fmt/ranges.h>

template <typename Cont>
bool parse_columns(std::string_view input, Cont& container,
                   unsigned required = 2) {
    namespace x3 = boost::spirit::x3;

    auto valid = [required](auto& ctx) {
        x3::_pass(ctx) = x3::_val(ctx).size() >= required;
    };

    auto delim = x3::char_('\t');
    auto field = *(~delim);
    auto rule
        = x3::rule<struct _, Cont, true>{"rule"} 
        = (field % delim)[valid];

    return parse(begin(input), end(input), rule, container);
}

int main() {
    for (auto input : {
             "",
             "\t",
             "abcd\t",
             "ab cd\tef",
             "\tef",
             "ab\tc\t\tdef",
             "abcd",
         }) {
        std::vector<std::string> columns;

        if (parse_columns(input, columns)) {
            fmt::print("'{}' -> {}\n", input, columns);
        } else {
            fmt::print("'{}' -> not matched\n", input);
        }
    }
}

版画

'' -> not matched
'   ' -> {"", ""}
'abcd   ' -> {"abcd", ""}
'ab cd  ef' -> {"ab cd", "ef"}
'   ef' -> {"", "ef"}
'ab c       def' -> {"ab", "c", "", "def"}
'abcd' -> not matched

调整

  • 要将重复的 \t 视为单个分隔符,只需将 field % delim 更改为 field % +delim
  • 你可以很容易地用另一个容器替换,比如std::set

Live On Compiler Explorer