How to split a string on multiple single‑character delimiters (and keep them) in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <vector>
#include <regex>

std::vector<std::string> split_keep_delims(
        const std::string& s,
        const std::string& delimiters)
{
    // Build a character class: e.g. ",;|" → "([,;|])"
    std::string pattern = "(" + delimiters + ")";
    std::regex re(pattern);

    std::sregex_token_iterator it(s.begin(), s.end(), re, {-1, 0});
    std::sregex_token_iterator end;

    std::vector<std::string> result;
    for (; it != end; ++it) {
        if (!it->str().empty())
            result.push_back(it->str());
    }
    
    return result;
}

int main() {
    auto parts = split_keep_delims("aa,bbb;cccc|ddddd", "[,;|]");

    for (auto& p : parts)
        std::cout << "[" << p << "] ";
}



/*
run:

[aa] [,] [bbb] [;] [cccc] [|] [ddddd] 

*/

 



answered Mar 8 by avibootz
0 votes
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>

std::vector<std::string> split_keep_delims(
        const std::string& s,
        const std::string& delimiters)
{
    std::unordered_set<char> delims(delimiters.begin(), delimiters.end());
    std::vector<std::string> result;

    std::string current;
    for (char c : s) {
        if (delims.count(c)) {
            if (!current.empty()) {
                result.push_back(current);
                current.clear();
            }
            result.push_back(std::string(1, c));  // keep delimiter
        } else {
            current += c;
        }
    }
    if (!current.empty())
        result.push_back(current);

    return result;
}


int main() {
    auto parts = split_keep_delims("aa,bbb;cccc|ddddd", "[,;|]");

    for (auto& p : parts)
        std::cout << "[" << p << "] ";
}



/*
run:

[aa] [,] [bbb] [;] [cccc] [|] [ddddd] 

*/

 



answered Mar 8 by avibootz

Related questions

...