我有以下字符串要测试:
"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:
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);
怎么了?
谢谢。
从文档(cppreference.com的std :: regex_search)开始:
std::regex_search
将成功匹配给定序列的任何子序列,而std::regex_match
仅当正则表达式匹配整个序列时才返回true 。
您可能需要检查std::match_results
,以检查在哪一点std::regex_match()
与完整序列完全不匹配(请参阅std::match_results::suffix()
和std::match_results::prefix()
成员)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句