这是我正在使用的代码
string exptype(string s)
{
string type = "";
try
{
if (regex_match(s.c_str(), regex("101(0|1)*111")))
type = "a valid binary";
else if (regex_match(s.c_str(), regex("[a-zA-Z]")))
type = "a valid combination of alphanum letters";
else if (regex_match(s.c_str(), regex("\([0-9]{3}\)[0-9]{3}-{0-9}{4}")))
type = "a valid phone number";
else if (regex_match(s.c_str(), regex("(19|20)[0-9][0-9]-(0[1-9]|1[0-2])- ((0[1-9])|([1-2][0-9])|(3[0-1]))")))
type = "a valid date";
else
type = "an invalid string";
}
catch (std::regex_error& e)
{
cout << e.code() << endl;
}
return type;
}然后我的主要内容如下:
int main()
{
string input;
do
{
cout << "Enter the string that will be validated.." << endl;
getline(cin, input);
if (input != "q")
{
cout << "This is " << exptype(input) << endl;
}
else
break;
} while (true);
return 0;
}上述代码有时会工作,有时它会抛出错误代码的异常:7我搜索了它,发现错误是error_brace:包含不匹配大括号({和})的表达式。我看不出这里有什么问题,任何帮助都是值得感激的。
谢谢
发布于 2015-10-16 19:45:10
正如马里亚诺所指出的,您应该用{0-9}替换[0-9]。在处理正则表达式或其他带有转义\的字符串时,最好使用原始字符串。例如:
regex_match(s.c_str(), regex(R"reg(\([0-9]{3}\)[0-9]{3}-[0-9]{4})reg"))https://stackoverflow.com/questions/33178152
复制相似问题