I'm trying to break apart a path formed by a series of folder names:
"/foldera/folderb/folderc"
into
"/foldera" "/folderb" "/folderc"
But I can't find out how to do this using std::regex,
{
std::regex exp("^(/[a-zA-Z0-9-_]+)+");
std::smatch res;
std::string str = "/uuu/kkk";
std::regex_search( str, res, exp ) ;
{
std::cout << res[0] <<";" << res[1] << std::endl;
}
std::cout << std::endl;
}
It will only match either the whole string or the last "/kkk",
I will never find the match "/uuu"
I know the problem is solvable with string split, but I'm interested in a std::regex solution here, because the above is doable with javascript and Qt's regex. But I don't know how to do it with std::regex.
PS. the following also doesn't work:
{
const std::string s = "/uuu/kkk";
std::regex words_regex("(/[a-zA-Z0-9-_]+)+");
auto words_begin =
std::sregex_iterator(s.begin(), s.end(), words_regex);
auto words_end = std::sregex_iterator();
std::cout << "Found "
<< std::distance(words_begin, words_end)
<< " words:\n";
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
std::cout << match_str << '\n';
}
}