拆分具有不同开始分隔符的字符串(不重复)

时间:2016-06-13 13:18:16

标签: c++

我需要拆分一个包含子串的大字符串!=""或者!""但我现在被困住了。

我的代码

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

基本上做我想要的,除了 - &gt; !=&lt; - 或 - &gt; !&#34; &lt; - 迷路了。

输入存储在字符串&#39;行&#39;

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"

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

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