拆分具有不同起始分度的字符串(不是重复的)

Splitting a string with different start delimeter (not a duplicate)

本文关键字:字符串 拆分      更新时间:2023-10-16

我需要拆分一个大字符串,其中包含由 !=" 或 !" 封装的子字符串,但现在我卡住了。

我有的代码

#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();
}