gpt4 book ai didi

c++ - 正则表达式误解或只是破坏了实现?

转载 作者:行者123 更新时间:2023-11-30 03:05:28 25 4
gpt4 key购买 nike

我尝试了一个非常简单的 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);
/* ... */
}

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

但是通过在 /* ... */ 处打印代码表示:

  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,没有变化。`

我对 regex_search 的理解有误吗? 我完全困惑了。还是只是 gcc?

最佳答案

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

欢迎志愿者;-)

[编辑] [自 gcc-4.9 起] 2将得到全面支持。

关于c++ - 正则表达式误解或只是破坏了实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7696063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com