提升正则表达式与选项卡不匹配
Boost regex don't match tabs
我使用的是boost regex_match,在不匹配制表符时遇到问题。我的测试应用程序如下所示:
#include <iostream>
#include <string>
#include <boost/spirit/include/classic_regex.hpp>
int
main(int args, char** argv)
{
boost::match_results<std::string::const_iterator> what;
if(args == 3) {
std::string text(argv[1]);
boost::regex expression(argv[2]);
std::cout << "Text : " << text << std::endl;
std::cout << "Regex: " << expression << std::endl;
if(boost::regex_match(text, what, expression, boost::match_default) != 0) {
int i = 0;
std::cout << text;
if(what[0].matched)
std::cout << " matches with regex pattern!" << std::endl;
else
std::cout << " does not match with regex pattern!" << std::endl;
for(boost::match_results<std::string::const_iterator>::const_iterator it=what.begin(); it!=what.end(); ++it) {
std::cout << "[" << (i++) << "] " << it->str() << std::endl;
}
} else {
std::cout << "Expression does not match!" << std::endl;
}
} else {
std::cout << "Usage: $> ./boost-regex <text> <regex>" << std::endl;
}
return 0;
}
如果我用这些参数运行程序,我不会得到预期的结果:
$> ./boost-regex "`cat file`" "(?=.*[^t]).*"
Text : This text includes some tabulators
Regex: (?=.*[^t]).*
This text includes some tabulators matches with regex pattern!
[0] This text includes some tabulators
在这种情况下,我本以为what[0].matched是false,但事实并非如此。
我的正则表达式有错吗
或者我必须使用其他格式/匹配标志吗?
提前谢谢!
我不确定你想做什么。我的理解是,你希望regex在文本中出现选项卡后立即失败。
您的正向前瞻断言(?=.*[^t])
在找到非制表符时即为true,并且您的文本中有很多非制表符。
如果你想让它失败,当有一个选项卡时,可以反过来使用否定的前瞻性断言。
(?!.*t).*
一旦找到选项卡,此断言就会失败。
相关文章:
- 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 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- 内联在对always_inline的调用中失败,Qt中的目标特定选项不匹配
- 提升正则表达式与选项卡不匹配