13

Why does this find no matches in g++ (Debian 4.6.3-1) 4.6.3 or clang version 3.2 (trunk 159457)

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string line("test");
    regex pattern("test",regex_constants::grep);
    smatch result;

    bool ret(false);
    ret = regex_search(line,result,pattern);  
    cout <<  boolalpha << ret << endl;
    cout <<  result.size() << endl;
    return 0 ;
}

output

false
0
ildjarn
  • 62,044
  • 9
  • 127
  • 211
user1385705
  • 141
  • 1
  • 4

3 Answers3

15

Because <regex> is not yet implemented in libstdc++, as documented here (ยง28).

For now, use Boost.Xpressive or Boost.Regex instead.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
3

By now your example runs correctly:

$ ~/src/gcc/inst/bin/g++ --version
g++ (GCC) 4.9.0 20140224 (experimental)

on

$ uname -a
Linux ... x86_64 x86_64 x86_64 GNU/Linux

executes

$ ./83-regex.x 
true
1
towi
  • 21,587
  • 28
  • 106
  • 187
2

According to this change log <regex> is supported in gcc-4.9 with libstd++-v3

http://gcc.gnu.org/gcc-4.9/changes.html

RiaD
  • 46,822
  • 11
  • 79
  • 123
Guy L
  • 2,824
  • 2
  • 27
  • 37