【问题标题】:Regular Expressions misunderstanding or just broken implementation?正则表达式误解或只是破坏了实现?
【发布时间】:2011-10-08 09:59:10
【问题描述】:

我尝试了regex_search 的一个非常简单的用法,但不明白为什么我没有得到匹配:

唉,gcc-C++0x-implementations 4.5 似乎不起作用,我收到链接错误here

但这是我的 gcc-4.7.0 尝试,非常简单:

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main () {
    regex rxWorld("world");
    const string text = "hello world!";
    const auto t0 = text.cbegin();
    smatch match;
    const bool ok = regex_search(text, match, rxWorld);
    /* ... */
}

我想我应该得到ok==true match 的东西。为此,我将示例简化为一个非常简单的正则表达式。我先尝试稍微复杂一些。

但是通过在/* ... */ 打印代码另有说明:

  cout << "  text:'" << text
       << "' ok:" << ok
       << " size:" << match.size();
  cout << " pos:" << match.position()
       << " len:"<< match.length();
  for(const auto& sub : match) {
      cout << " ["<<(sub.first-t0)<<".."<<(sub.second-t0)
           << ":"<<sub.matched
           << "'"<<sub.str()
           << "']";
  }
  cout << endl;

输出是:

$ ./regex-search-01.x
text:'hello world!' ok:0 size:0 pos:-1 len:0

更新:我也试过regex_search(t0, text.cend(), match, rxWorld)const char* text,没有变化。 `

我对@9​​87654332@的理解错了吗?我完全被迷惑了。还是只是 gcc?

【问题讨论】:

  • 我自己没有使用过新的 regexp 东西,但是根据快速谷歌,您使用的是带有错误参数的 regex_match。 johndcook.com/cpp_regex.html
  • 是的,我也使用该教程来磨练我的知识。这些函数有一堆重载。我的 string 应该也可以,但也许我也应该尝试使用迭代器。等等……不,没有变化。
  • 嗯.....我认为可能有过载。这是有道理的,因为它正在编译。嗯....
  • 我在 Boost 1.47 和 GCC 4.6.1 上进行了尝试,得到了text:'hello world!' ok:1 size:1 pos:6 len:5 [6..11:1'world']
  • @kerrek:谢谢!那我怀疑gcc。如果您愿意,请回答您的发现。

标签: c++ regex gcc c++11


【解决方案1】:

C++-0x status of libstdc++ 可以看出,正则表达式支持不完整。 特别是 match_results 没有完成。迭代器甚至都没有启动。

欢迎志愿者 ;-)

[编辑] [截至 gcc-4.9]2 将被完全支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2016-08-31
    • 1970-01-01
    • 2021-07-16
    • 2012-05-21
    • 2011-10-10
    • 2013-04-12
    相关资源
    最近更新 更多