c++ - 为什么 boost 正则表达式 '.{2}' 不匹配 '??'

标签 c++ boost boost-regex trigraphs

如果数据流中的有趣数据,我正在尝试匹配一些 block 。

应该有一个前导<然后是四个字母数字字符、两个校验和字符(或 ?? 如果未指定 shecksum)和尾随 > .

如果最后两个字符是字母数字,则以下代码按预期工作。如果他们是 ??尽管它失败了。

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data??>bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

我在 the documentation 中没有发现任何东西这表明应该是这种情况(除了 NULL 和换行符之外的所有内容都应该匹配 AIUI)。

那么我错过了什么?

最佳答案

因为 ??> 是一个 trigraph ,它会被转换成,你的代码相当于:

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data}bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

你可以改成这样:

std::string haystack = "Fli<data?" "?>bble";

Demo (注意:我使用的 std::regex 大致相同)

注意:三字母表已从 C++11 中弃用,将(可能)从 C++17 中删除

关于c++ - 为什么 boost 正则表达式 '.{2}' 不匹配 '??',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483766/

相关文章:

c++ - 从 C++11 中的容器元组中提取 value_type 的元组

sockets - boost asio SO_REUSEPORT

visual-c++ - 使用 MSVC _MANAGED (C++/CLI) 在非 C++03 模式下编译 Boost.Regex 1.76?

c++ - 使用 Boost C++ 库将正则表达式替换为自定义替换

c++ - 从 int 到 char* 的看似不正确的转换 - 如何查看问题

c++ - Variadic 类模板和继承 - 默认编译器生成的构造函数

c++ - Boost Asio - 如何知道处理程序队列何时为空?

c++ - boost 示例未能构建

c++ - 如何防止为未实现方法的对象生成模板

c++ - QThread::currentThread () 与 QObject::thread()