The following code in VS2015 displays x , but for some reason the result in ideone is x1 . Why??
#include <iostream> #include <string> #include <regex> using namespace std; int main() { string s = "x"; s = regex_replace(s + "0123456789", regex("(\\b(?=9.*(1))|0(?=.*(1))|1(?=.*(2))|2(?=.*(3))|3(?=.*(4))|4(?=.*(5))|5(?=.*(6))|6(?=.*(7))|7(?=.*(8))|8(?=.*(9))|9(?=.*(0)))(?=9*-)|\\d{10}$"), "$2$3$4$5$6$7$8$9$10$11$12"); cout << s << '\n'; return 0; } In the given example, the coincidence should go only along the last branch of |\\d{10}$ , i.e. all groups should be empty. T. o. The added tail of 10 digits should be removed. But from somewhere the number 1 is taken. Why is this happening? And how to fix it?
Reduced the example
http://ideone.com/THHiay
#include <iostream> #include <string> #include <regex> using namespace std; int main() { string s = "x"; s = regex_replace(s + "0123456789", regex("0(?=.*(1))(?=9*-)|\\d{10}$"), "<$1>"); cout << s << '\n'; return 0; } x<1> However, other zeros in the string do not deteriorate (they change only before the nines and the hyphen)
http://ideone.com/XcFF6M
#include <iostream> #include <string> #include <regex> using namespace std; int main() { string s = "100#100-100,101#101-101,109#109-109"; s = regex_replace(s + "0123456789", regex("0(?=.*(1))(?=9*-)|\\d{10}$"), "<$1>"); cout << s << '\n'; return 0; } 100#10<1>-100,101#101-101,109#1<1>9-109<1> Meanwhile, VS for the last example gives
100#10<1>-100,101#101-101,109#1<1>9-109<> Which is logical - there is no group, but there is a replacement with erasing the tail.
?=with?:we get the expected result), so we can assume that this is a compiler bug - BOPOH