CPP 中带有多个分隔符的分割线
split line in cpp with multiple delimiter
我想按<=>
或=>
的外观拆分每一行 所以我有 2 个分隔符,每个分隔符都有不止一个字符
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
那么,让我们看看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"
相关文章:
- 是默认情况下分配给char数组常量的值
- 当用户键入分隔符时,停止getline()输入
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 带有多个字符分隔符的正则表达式
- 在C++中将变量 wstring 分配给 Char 数组
- 内存错误低于在C++年实现埃拉托色尼筛分时的预期
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 字符串开头的分隔符
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 如何将内联匿名函数分配给C++函数指针
- 指针问题:从不兼容的类型"int"分配给"int *"
- 尝试将 c 字符串数组与分隔符连接起来
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- Cython:无法分配给 2D 矢量
- 如何将对象的向量分配给堆
- 使用 BOOST 分词器显示分隔符,并且不对引号中的字符串进行分词
- CPP 中带有多个分隔符的分割线