Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will regex "([a-z])((?!\\1)[a-z])" match string "aa" in C++11?

Tags:

c++

regex

c++11

Indeed running

regex pattern("([a-z])((?!\\1)[a-z])");
cout << regex_match("aa", pattern) << endl;

results in the output "1", yet the first group matched is "a", and if instead I run

regex pattern("([a-z])((?!a)[a-z])");
cout << regex_match("aa", pattern) << endl;

I get "0" as expected.

Note that exactly the same regex works as expected in Python 3, i.e. running

re.match("([a-z])((?!\\1)[a-z])", "aa")

produces no matches. Can someone explain what is happening here?

Just for reference, I am using g++ (tdm64-1) 5.1.0 with flags -O2 and -std=c++11.

like image 713
Fidel I. Schaposnik Avatar asked Oct 21 '16 18:10

Fidel I. Schaposnik


1 Answers

Because it was bugged in GCC5.
It works as expected in GCC6 and MSVC14.

like image 189
Jan Hohenheim Avatar answered Sep 28 '22 05:09

Jan Hohenheim