开发者

Regular Expressions misunderstanding or just broken implementation?

I tried a very simple use of regex_search and can not understand w开发者_运维知识库hy I do not get a match:

Alas, the gcc-C++0x-implementations 4.5 does not seem to be working, I get a link error here.

But here is my gcc-4.7.0 try, quite straightforward:

#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);
    /* ... */
}

I think I should get ok==true and something in match as well. I reduced the example to a very simple regex for this. I tried slightly more complicated first.

But by printing code at /* ... */ says otherwise:

  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;

The output is:

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

Update: I also tried regex_search(t0, text.cend(), match, rxWorld) and const char* text, no change. `

Is my understanding of regex_search wrong? I am completely baffled. Or is it just the gcc?


As you can see from the C++-0x status of libstdc++ the regex support is incomplete. In particular match_results are not finished. Iterators are not even started.

Volunteers are welcome ;-)

[EDIT] [As of gcc-4.9]2 will be fully supported.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜