1

I tried a very simple use of regex_search and can not understand why 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?

towi
  • 21,587
  • 28
  • 106
  • 187
  • Have not used the new regexp stuff myself, but based on a quick google, you are using regex_match with the wrong parameters. http://www.johndcook.com/cpp_regex.html – Corbin Oct 08 '11 at 10:03
  • Yes, I used that tutorial to hone my knowledge, too. The functions have a bunch of overloads. Mine with a `string` should work as well, but maybe I should try with iterators, too. Wait... no, no change. – towi Oct 08 '11 at 10:55
  • Hmmm..... I figured there might be overloads. Which makes sense since it's compiling. Hmmm.... – Corbin Oct 08 '11 at 10:58
  • 3
    I tried this with Boost 1.47 and GCC 4.6.1 and got, `text:'hello world!' ok:1 size:1 pos:6 len:5 [6..11:1'world']`. – Kerrek SB Oct 08 '11 at 11:38
  • @kerrek: Thanks! I suspect gcc, then. Make an answer of your finding, if you like. – towi Oct 08 '11 at 12:10
  • I tried this with MSVC++ 2010 and the included `tr1::regex`. It produced the same output that Kerrek SB shows. – Blastfurnace Oct 09 '11 at 05:34

1 Answers1

3

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.

emsr
  • 15,539
  • 6
  • 49
  • 62