3

Suppose I would like to do extract the contents of matching curly braces using C++11 regex. So, for example, {foo} would match successfully and I could the use the match_result to extract the contents. It seems simple, but the following code does not quite do what I wish

std::string foo("{foo}");
std::regex r("\\{(.*)\\}");
std::smatch m;
bool result = regex_match(foo, m, r); // result returns true

cout << m[0] << endl; // prints: {foo}
cout << m[1] << endl; // prints: {foo} instead of just foo as I would expect

Now shouldn't m[1] return just foo without the braces given that it is the first capture group?

EDIT: An essential piece of information for this question is that the compiler I'm using is GCC 4.6.3 (which currently is the latest repository version in Ubuntu 12.04 LTS). The answer identifies precisely how poor the support for regex in GCC is.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Alan Turing
  • 12,223
  • 16
  • 74
  • 116

1 Answers1

3

Your pattern is correct. Your code is largely correct with few missing 'std'' here and there (for 'cout', 'endl' and regex_match), or at least inconsistent (assuming you are 'using namespace std').

Moreover, on Visual Studio 2012, your code output the expected result. I didn't try 2010, but I suspect it is running there as well (Microsoft incorporated TR1 back in 2010).

I suspect you are using gcc. As @Artyom pointed out, in gcc/libstdc++ isn't implemented. It compiles fine with no warnings but it gives the wrong results. Despite the common belief that gcc is superior than Microsoft in every area, this isn't the case in regular expression.

Find status of regex on gcc here: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x

Uri London
  • 10,631
  • 5
  • 51
  • 81
  • You're absolutely correct. GCC still has poor support for `std::regex`. It's unfortunate that everything would compile without errors given that this is the case. I guess I need to switch to boost for regex for now. – Alan Turing Nov 05 '12 at 14:25
  • Microsoft added TR1 in 2008, as a feature-pack precursor to VC++ 2008 SP1 (then incorporated into said service pack). – ildjarn Nov 06 '12 at 23:43
  • 5 years layer g++ v4.8 is the default g++ version on Ubuntu 14 which has Long Term Support for years to come. And, apparently, regex compiles but doesn't work. Argh. See https://stackoverflow.com/a/12665408/2950621 for more about limitations of v4.8 regex support. – nmgeek Nov 25 '17 at 20:29