Regex不匹配,除非整行都相同
Regex not matching unless the entire line is the same
我的regex有问题。例如,在以下代码中,if语句返回false:
string test("ABC123");
regex e("123");
if(regex_match (test.begin(), test.end(), e))
{
//do something
}
使正则表达式返回true的唯一方法是将正则表达式设置为"ABC123"
或".+"
。其他可能的正则表达式,如"[0-9]"
或"[A-Z]"
也返回false。
否,请参阅以下解释:
整个目标序列必须与此的正则表达式匹配函数返回true(即,没有任何附加字符在比赛之前或之后)。对于当匹配只是序列的一部分,请参见regex_search。
请改用regex_search
。
返回true
:
string test("ABC123");
regex e("123");
if(regex_search (test.begin(), test.end(), e))
{
return true;
}
您应该使用
regex_search(test.begin(), test.end(), e))
而是
只有当测试的整个字符串与regex匹配时,regex_match才会返回true。否则,如果字符串中的子字符串与您的匹配,regex_search将返回true。
请查看此链接了解更多信息:
http://www.johndcook.com/blog/cpp_regex/
相关文章:
- 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 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- Regex不匹配,除非整行都相同
- Regex:返回完整字符串和不匹配的组
- std::regex与工作表达式不匹配
- C++ <regex> 搜索不匹配