正则表达式与 c++ 不匹配
Regex not matching c++
我有这个字符串要测试:
"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
。
我已经在这里测试了我的正则表达式:https://www.debuggex.com/,它可以工作。
我也尝试了match_any
常量:
regex_match(testStatement, check, createTableInside, match_any);
怎么了?
谢谢。
来自文档(std::regex_search 来自 cppreference.com):
std::regex_search
将成功匹配给定序列的任何子序列,而std::regex_match
只有在正则表达式匹配整个序列时才返回 true。
您可能需要检查std::match_results
,以检查std::regex_match()
在哪个点未能匹配完整的序列(请参阅std::match_results::suffix()
和std::match_results::prefix()
成员)。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配