我要测试这个字符串:
"CPF char,
Nome char [80],
Idade int,
Salario double,
Sexo char,
Tem_Filhos bool,"
或者简单地说:
string testStatement("CPF char,\nNome char [80],\nIdade int,\nSalario double,\nSexo char,\nTem_Filhos bool,");
这是我的注册表:
regex createTableInside("(?:[ \\n\\t]*)(\\w*)(?:[ \\n\\t]+)(?:(?:(char)[ \\n\\t]*\\[[ \\n\\t]*(\\d*)[ \\t\\n]*\\])|(char)|(int)|(double)|(bool)|(bloob))(?:[ \\n\\t]*)(,)");
当我执行regex_search
时,它会找到所有内容:
while(regex_search(testStatement, check, createTableInside))
{
for(index = 1; index < check.size(); index++)
{
if(check.str(index).compare("") == 0) continue;
cout << "\"" << check.str(index) << "\"" << endl;
}
aux = check.suffix();
}
输出:
"CPF"
"char"
","
"Nome"
"char"
"80"
","
"Idade"
"int"
","
"Salario"
"double"
","
"Sexo"
"char"
","
"Tem_Filhos"
"bool"
","
但当我执行regex_match
时,check.size()
始终返回0
。
我在这里测试了我的regEx:https://www.debuggex.com/,它确实有效。
我也试过match_any
常数:
regex_match(testStatement, check, createTableInside, match_any);
怎么了?
感谢。
答案 0 :(得分:2)
来自docs (std::regex_search from cppreference.com):
std::regex_search
将成功匹配给定序列的任何子序列,而std::regex_match
仅在正则表达式与整个序列匹配时才返回true 。
您可能需要检查std::match_results
,以检查确切std::regex_match()
未能与完整序列匹配的位置(请参阅std::match_results::suffix()
和std::match_results::prefix()
成员)。< / p>