【问题标题】:Match BOL and EOL with std::regex将 BOL 和 EOL 与 std::regex 匹配
【发布时间】:2013-09-02 10:35:09
【问题描述】:
std::string string("0");
std::regex pattern("^0$",std::regex_constants::extended);
bool match(std::regex_match(string,pattern));

我相信^$ 必须分别匹配 BOL 和 EOL。因此match 应该是true,但实际上是false。我错过了什么吗?


gcc-4.8.1 live demo.

【问题讨论】:

  • 哪个编译器? std::regex 未在 g++ afaik 中实现。在大多数情况下,^/$ 匹配字符串的开始/结束,除非使用多行标志/设置。
  • @Qtax 我相信它是在 gcc-4.8.1 中实现的。在帖子中提供了现场演示链接。
  • 仅仅因为它编译并不意味着它已经完全实现和工作。

标签: c++ regex gcc boost std


【解决方案1】:

libstdc++ 不完全支持正则表达式(您可以检查它here)。 我尝试使用带有 libc++-3.2 的 clang 3.2 编译此代码,结果为“true”。使用 libc++ 或 boost。

尤其是 libstdc++ 正则表达式的实现状态

8   Regular expressions
28.1    General N    
28.2    Definitions N    
28.3    Requirements    N    
28.4    Header <regex> synopsis N    
28.5    Namespace std::regex_constants  Y    
28.6    Class regex_error   Y    
28.7    Class template regex_traits Partial  
28.8    Class template basic_regex  Partial  
28.9    Class template sub_match    Partial  
28.10   Class template match_results    Partial  
28.11   Regular expression algorithms   N    
28.12   Regular expression Iterators    N    
28.13   Modified ECMAScript regular expression grammar  N

【讨论】:

  • 感谢您的回答。我想知道,这些令牌实现属于哪个部分? 28.1 General 可能?天哪,他们为什么不对not_implemented_yet 异常表示友好...
  • @Kolyunya 不知道。例如对于 \\d 正则表达式,它会抛出 std::regex_error。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 2017-05-12
  • 2011-08-30
相关资源
最近更新 更多