Regex oddity

Hello, running this test :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// match_results::begin/end
// - using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("subject");
  std::smatch m;
  std::regex e ("(sub)(.*)");

  std::regex_match ( s, m, e );

  std::cout << "matches:" << std::endl;
  for (std::smatch::iterator it = m.begin(); it!=m.end(); ++it) {
	std::cout << *it << std::endl;
  }
  return 0;
}


On gcc 4.7.2 with c++11, it will return :

"subject"
"sub"

and the wrong one :
"bject"

Why is this b coming in ? I just copied and pasted the official code. Tried other languages and the third result should be "ject"

Something odd going on.

Any clue ?

Thanks,

Larry
Do not use std::regex with GCC's libstdc++.

See section 28 in
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011

Use boost regex instead (Replace std:: with boost::)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main ()
{
  std::string s ("subject");
  boost::smatch m;
  boost::regex e ("(sub)(.*)");

  boost::regex_match ( s, m, e );

  std::cout << "matches:" << std::endl;
  for ( auto it = m.begin() ; it!=m.end() ; ++it )
      std::cout << *it << std::endl;
}
Done and solved :D

Thanks !
Topic archived. No new replies allowed.