我使用std :: string regex_search和std :: string regex_replace查找子字符串并替换。我的问题是我不知道如何用正则表达式来表达它。当涉及到yy->%y时,其他情况如y,yyy,yyyy,yyyyy等。->%Y
std::string text = "y yyaa";
std::regex y_re("[yY]+"); // this is the regex that matches y yyy or more yyyy
std::regex yy_re("(?=^y{2}[^y])y{2}"); // this is the regex that matches only yy- my problem is here
std::string output = "";
smatch m;
if (regex_search(text,m,yy_re)) {
output = std::regex_replace(text,yy_re,"%y");
}
else {
output = std::regex_replace(text,y_re,"%Y");
}
cout << output << endl;
我的实际输出:
%Y %Yaa
我的预期输出:
%Y %yaa