RegexReplace different output according to compiler

Viewed 73

LIVE

#include <iostream>
#include <regex>

int main()
{
    std::string text = R"(11111111111111111111
11111111111111111111
11111111111111111111
11111111111110000000
11111111111000000000
11111111100011111100
11111111100111100000)";

    std::regex re("^1+\n");
    std::string str = std::regex_replace(text, re, "");
    std::cout << str;
  return 0;
}

Why does the exact same code when compiled under visual studio, trims all lines containing 11111111111111111111?

I would like to it behave the same as on the link above, replacing just the first occurrence.

Do i need any kind of 'special' flag into the regex?

1 Answers

I modified my answer. It might be a compiler difference. You can refer to the flags and similar thread.

std::regex_constants::match_flag_type fonly =
    std::regex_constants::format_first_only;
std::regex re("^1+\n");
std::string str = std::regex_replace(text, re, "", fonly);
Related