Stack overflow during std :: regex_replace

I am trying to execute the following C ++ STL based code to replace text in a relatively large SQL script (~ 8MB):

std::basic_regex<TCHAR> reProc("^[ \t]*create[ \t]+(view|procedure|proc)+[ \t]+(.+)$\n((^(?![ \t]*go[ \t]*).*$\n)+)^[ \t]*go[ \t]*$");
std::basic_string<TCHAR> replace = _T("ALTER $1 $2\n$3\ngo");
return std::regex_replace(strInput, reProc, replace);

The result is a stack overflow, and it is difficult to find information about this particular error on this particular site, as this is also the name of the site.

Edit: I am using Visual Studio 2013 update 5

Edit 2: The source file has more than 23,000 lines. I cut the file to 3500 lines and still get the error. When I cut it another 50 lines to 3,456 lines, the error will disappear. If I include only these lines in a file, the error will still disappear. This suggests that the error is not related to a specific text, but too much.

Edit 3: Here is a complete working example: https://regex101.com/r/iD1zY6/1 However, it does not work in this STL code.

+4
source share
2 answers

, STL Perl ( 100 , fooobar.com/questions/94440/...), , -, STL/++, . (, ++/STL , , , , ++, , ). , , , , :

   std::basic_string<TCHAR> result;
   std::basic_string<TCHAR> line;
   std::basic_regex<TCHAR> reProc(_T("^[ \t]*create[ \t]+(view|procedure|proc)+[ \t]+(.+)$"), std::regex::optimize);
   std::basic_string<TCHAR> replace = _T("ALTER $1 $2");

   do {
      std::getline(input, line);
      int pos = line.find_first_not_of(_T(" \t"));
      if ((pos != std::basic_string<TCHAR>::npos) 
          && (_tcsnicmp(line.substr(pos, 6).data(), _T("create"), 6)==0))
         result.append(std::regex_replace(line, reProc, replace));
      else
         result.append(line);
      result.append(_T("\n"));
   } while (!input.eof());
   return result;
+1

20% regex101 (. ).

\\bcreate[ \t]+(view|procedure|proc)[ \t]+(.+)\n(((?![ \t]*go[ \t]*).*\n)+)[ \t]*go[ \t]*

:

  • :
  • db - script.
  • , ( - escape- , )

, ...

  • create ... ,

  • create ..., go (, go)

... :

std::basic_regex<TCHAR> reProc("\bcreate[ \t]+(view|procedure|proc)");
std::basic_string<TCHAR> replace = _T("ALTER $1");
return std::regex_replace(strInput, reProc, replace);

( - - 1/4).

+2

All Articles