mercredi 30 novembre 2016

Having issues splitting/formatting a string into a vector based on multiple criteria

I need to split a series of strings that are formatted like the following 2 strings:

<verb> sigh <adverb> ; portend like <object> ; die <adverb> ;

<start> The <object> <verb> tonight. ;

into a vector in which the first nonlimiter (ex:) is the first element of the vector, and then the rest of the string is broken up into elements by the semicolons. So the first example string should be broken up like this when I'm finished:

newvec.at(0)= <verb>
newvec.at(1) = sigh <adverb>
newvec.at(2) = portend like <object>
newvect.at(3) = die <adverb>

I was provided with the following function to break up strings into vectors using regexs as delimiters and it worked fine when I previously needed it, but I'm having trouble figuring out how to use it in this case if I even can.

/**
 * Splits the input string based on the (string) regular expression.
 *
 * @param input the string to split.
 * @param regex a string for the regular expression to use to split.
 * @param delim if true, then the regex chars will be returned in the split,
 *              if false, the regex chars are removed.
 * @return a vector of strings that is the input string split based on the
 *         given regular expression.
 */
vector<string> split(const string &input, const string &regex, bool delim = true) {
    std::regex re(regex);

    std::sregex_token_iterator first, last;
    if (delim) {
        first = sregex_token_iterator{input.begin(), input.end(), re};
    } else {
        // the -1 removes the delimiter
        first = sregex_token_iterator{input.begin(), input.end(), re, -1};
    }
    return vector<string>(first, last);
}

Calling new_vec = split(ex_string, ";", false) in my function will split the string into a vector based on the semicolons (and remove the semicolons), but I'm unsure of how to make the first non limiter the first element of the vector. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire