在C++中使用非常简单的表达式regex_error

regex_error in C++ with really simple expression

本文关键字:表达式 regex error 简单 非常 C++      更新时间:2023-10-16

我有一组字符串,比如这个紫罗兰+2,橙色-3等等。这是一种命名颜色的方法。我想使用以下代码获取regex的颜色、操作和编号:

int parseColor(string color){
smatch sm;
int ret = -1;
    if (regex_match(color,sm,regex("(.*)([+-])(.*)")))
        ret = s_colors[sm[1].str()]+s_colors[sm[2].str()]*atoi(sm[3].str().c_str());
return ret;
}

其中s_colors是一个映射,它将每种颜色与一个整数值相匹配,将+、-符号与+1和-1相匹配。

我也试过用\\+和\\-来修饰+-符号,但也不起作用。每次运行时,我都会得到regex_error。在ruby中尝试这个似乎有效,所以…我不确定regex的错误是什么。

Gcc 4.8.2不支持regex。您需要4.9