【发布时间】: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,没有变化。
`
我对@987654332@的理解错了吗?我完全被迷惑了。还是只是 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。如果您愿意,请回答您的发现。