【发布时间】:2020-04-22 22:49:50
【问题描述】:
我有几个单词,并希望将这些单词放在向量中,这样我就可以将每个单词作为一个单独的对象进行操作。问题是当我在找到第一个匹配项后使用函数 regexp_search(string,match,regexp) 我试图删除在找到匹配词之前字符串中的所有内容但调用函数 match.suffinx().str() 时返回 emply 字符串它应该从该字符串返回其他单词。
这是我的正则表达式:
std::regex reg("([C-Fc-f]at)");
这是我的功能:
std::vector<string> stringToVector(string str, regex reg){
cout << boolalpha;
vector<string> vec;
smatch match;
while (regex_search(str, match, reg))
{
vec.push_back(match.str(1));
str = match.suffix().str();
}
return vec;}
【问题讨论】:
-
您能否提供一些示例字符串以及要推回向量的预期子字符串?
-
ye 例如我粘贴字符串:“Cat fat rat bat”函数将“Cat”推入向量内部,但之后的匹配后缀等于“”(空字符串)that
s why function stops working and doesnt push向量中的“胖”