#include <iostream>
#include <regex>
#include <string>
#include <vector>
/*
split_regex(s, pattern)
-----------------------
Splits a string using a regular expression pattern.
This uses:
- std::regex
- std::sregex_token_iterator with submatch index -1
which returns the text *between* regex matches.
Example:
pattern = ",+"
"a,,b,c" → ["a", "b", "c"]
*/
std::vector<std::string> split_regex(const std::string& s, const std::string& pattern) {
std::regex re(pattern);
std::sregex_token_iterator it(s.begin(), s.end(), re, -1);
std::sregex_token_iterator end;
return {it, end};
}
int main() {
std::string input = "Just-when--I--thought--I-was--out,-----They----pull-me-back-in.";
std::string pattern = "-+"; // The regex pattern -+ means one or more hyphen
auto parts = split_regex(input, pattern);
std::cout << "\nSplit result:\n";
for (const auto& p : parts) {
std::cout << "[" << p << "]\n";
}
}
/*
run:
Split result:
[Just]
[when]
[I]
[thought]
[I]
[was]
[out,]
[They]
[pull]
[me]
[back]
[in.]
*/