我试图从单个正则表达式匹配中获取多个变量。
std::string string_to_search = "abc def ghi";
std::regex pattern = "abc (.+) (.+)";
regex_search(string_to_search);
std::string segment1 = //assign to first [a-z]*
std::string segment2 = //assign to second [a-z]*
在红宝石中,我想做的是
case string_to_search
when /\Aabc (.+) (.+)/
segment1 = $1
segment2 = $2
end
答案 0 :(得分:1)
这两组可以如下获得:
std::string string_to_search = "abc def ghi";
std::regex pattern ("abc (.+) (.+)");
std::smatch base_match;
if (std::regex_match(string_to_search, base_match, pattern))
{
std::cout << base_match[1] << endl; // def
std::cout << base_match[2] << endl; // ghi
}
regex_match的说明及其使用方法是here。