0

I am using C++ regex. was not able to grasp the following programming output.

#include <iostream>
#include <regex>
#include <algorithm>
#include <string>
using namespace std;

int main(){
  regex r("a(b+)(c+)d");    
  string s ="abcd";
  smatch m; 
  cout << s << endl;
  const bool b = regex_match(s,m, r);
  cout << b <<endl; // prints 1 - OK
  if(b){
    cout << m[0] << endl; // prints abcd - OK 
    cout << m[1] << endl; // prints ab - Why? Should it be just b?
    cout<<  m[2] << endl; // prints bc - Why? Should it be just c?
  }

}

I per my exposure to regex in other languages, the parenthesis should match the captured part of the string? so the output should be

1
abcd
b
c

EDIT: I am using g++ 4.6

David
  • 4,634
  • 7
  • 35
  • 42

1 Answers1

3

Assuming you are using g++, you should note that its implementation of <regex> (section 28) is incomplete. Note the listings for basic_regex, sub_match, and match_results are declared "Partial".

For more info on g++, I think this post from a year ago is still relevant (as is this bug report).

This would explain why it's not giving the results that you expect. You may wish to try Boost regex in the meantime.

Community
  • 1
  • 1