拆分具有不同起始分度的字符串(不是重复的)
Splitting a string with different start delimeter (not a duplicate)
我需要拆分一个大字符串,其中包含由 !=" 或 !" 封装的子字符串,但现在我卡住了。
我有的代码
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using namespace boost;
std::string line;
// Create vector to store matrix
std::vector< std::vector<string> > vec_line;
// Create temp vector to create "rows"
vector<string>vec_string_temp;
string add2vec_ele(string firste, string line)
{
// Add row
vec_string_temp.push_back(firste);
boost::algorithm::split_regex( vec_string_temp, line, regex( "(!="|!")" ) ) ;
// store row in vec_line
vec_line.push_back(vec_string_temp);
vec_string_temp.clear();
return string();
}
int main()
{
string firste = "KeyWord";
string line = "!="abcd!#efg" !"ABCDEFGHAG!/8765438" !"This !/[isanotherstring]?but nobody cares78" !="again a string with equal sign and exclamation mark"";
add2vec_ele(firste,line);
// print all elements
for (unsigned int i = 0; i < vec_line.size(); i++)
{
std::cout << "Vector line: " << i << " ";
for (unsigned int j = 0; j < vec_line[i].size(); j++)
{
std::cout << " Col: " << j << " " << vec_line[i][j];
}
std::cout << endl;
}
}
基本上可以做我想做的事,除了 -> != <- 或 ->!" <- 丢失了。
输入存储在字符串"行"中
string line = "!="abcd!#efg" !"ABCDEFGHAG!/8765438" !"This !/[isanotherstring]?but nobody cares78" !="again a string with equal sign and exclamation mark"";
上面代码的输出是
Vector line: 0 Col: 0 Col: 1 abcd!#efg" Col: 2 ABCDEFGHAG!/8765438" Col: 3 This !/[isanotherstring]?but nobody cares78" Col: 4 again a string with equal sign and exclamation mark"
预期输出为
Vector line: 0 Col: 0 Col: 1 !="abcd!#efg" Col: 2 !"ABCDEFGHAG!/8765438" Col: 3 !"This !/[isanotherstring]?but nobody cares78" Col: 4 !="again a string with equal sign and exclamation mark"
我怎样才能做到这一点?
使用 Perl Regex(展望未来)解决了我的问题:
string add2vec_ele(string firste, string line)
{
// Add row
vec_string_temp.push_back(firste);
boost::regex ex( "(?<!^)(?:(?=!=")|(?=!"))", boost::regex::perl );
boost::algorithm::split_regex( vec_string_temp, line, ex ) ;
// store row in vec_line
vec_line.push_back(vec_string_temp);
vec_string_temp.clear();
return string();
}
相关文章:
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 将字符串拆分为等长 c++ 的部分
- 如何使用 Boost C++ 打印第 n 个字符串拆分?
- 我在将字符串拆分为向量时遇到困难
- C++ 如何将字符串拆分为单个字符
- 是否有用于执行反向字符串拆分器的开箱即用功能?
- 如何将字符串拆分为一组 3 个字符,它们之间有空格
- 将字符串拆分为单个单词并将它们放置在向量中
- 基于其中的数字子字符串拆分字符串
- 如何在C++中将数字字符串拆分为数组
- 如何在 c++ 中将字符串拆分为字符串 2d 数组数组
- 如何将标准::字符串拆分为标准::string_views的范围 (v3)
- 将特定格式的字符串拆分为浮点数和字符串
- 在 c++ 中将字符串拆分为单独的变量
- 用C 中的4个字符将字符串拆分
- 如何用Arduino中的另一个字符串将字符串拆分
- 如何在 Main 方法之外将字符串拆分为标记
- C 将字符串按空白空间将字符串拆分,除非将其封闭在引号中并存储在向量中
- 将字符串拆分为矢量 C++
- 试图将字符串拆分为两个整数