【问题标题】:split line in cpp with multiple delimiter [duplicate]具有多个分隔符的cpp中的分割线[重复]
【发布时间】:2014-01-13 13:31:50
【问题描述】:

我想通过 <=>=> 的出现来分割每一行所以我有 2 个分隔符,每个分隔符都有多个字符 p>

  string example="A + B => C + D";
  vector <string> leftRight;
  boost::algorithm::split_regex( leftRight, example, boost::is_any_of( " <=> +| => " ));

我的预期结果是这样的:

leftright[0]= A+B
leftright[1]= C+D

【问题讨论】:

  • 这是不同的我正在寻找一种由多个分隔符分割的方法

标签: c++ boost split


【解决方案1】:

那么,让我们看看boost::algorithm::split_regex。在最后一个参数之前你做得很好。该函数需要一个 boost::regex 作为最后一个参数,而 boost::is_any_of 不提供其中之一。

您的用例的合理正则表达式应该是这样的:

boost::regex r("(<=>)|(=>)");

如果我们把所有这些放在一起:

#include <vector>
#include <string>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>

int main() {
    std::string example="A + B => C + D";
    std::vector <std::string> leftRight;
    boost::regex r("(<=>)|(=>)");
    boost::algorithm::split_regex(leftRight, example, r);

    for (int i=0; i<leftRight.size(); ++i)
        std::cout << "\"" << leftRight[i] << "\"\n";
}

我们得到以下输出:

"A + B "
" C + D"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2018-01-15
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多