【问题标题】:Why doesn't boost regex '.{2}' match '??'为什么不提升正则表达式 '.{2}' 匹配 '??'
【发布时间】:2017-03-21 21:26:54
【问题描述】:

如果数据流中有有趣的数据,我会尝试匹配一些块。

应该有一个前导<,然后是四个字母数字字符、两个校验和字符(如果没有指定校验和,则为??)和一个尾随>

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

// 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 匹配)。

那么我错过了什么?

【问题讨论】:

  • 你用的是什么编译器?我的 (gcc) 给出了一个明确的警告,说“trigraph ??> convert to }”。
  • 我正在使用带有 2008 工具链的 Visual Studio 2013。

标签: c++ boost boost-regex trigraphs


【解决方案1】:

因为??&gt;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 中删除

【讨论】:

  • 你明白了。非常有趣 - 我以前没听说过三元组!
  • 它们在最新标准中被删除(或弃用?)
  • @sehe 从 C++11 中弃用,将被 C++17 删除
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2011-06-07
相关资源
最近更新 更多