3

why does this piece of C++ code block forever?

string word = " a\n";
regex indent("^( |\t)*");
word = regex_replace(word, indent, "");

and why does this piece of C++ code terminate quickly?

string word = " a\n";
regex indent("^( |\t)+");
word = regex_replace(word, indent, "");

and to add one more twist why does this terminate quickly?

string word = " a\n";
regex indent("^( |\t)+?");
word = regex_replace(word, indent, "");

I would expect that "^( |\t)+?" would be the same as "^( |\t)*"

I am using libc++ and llvm and the standard c++ regex library.

Andrew Hoos
  • 1,510
  • 2
  • 15
  • 23
  • Last I heard libstdc++'s `` is not complete. [See this answer to a related question.](http://stackoverflow.com/a/12665408/445976) – Blastfurnace Aug 06 '13 at 03:06

3 Answers3

3

My guess would be that ^( |\t)* matches nothing (i.e. the * means 0 or more so it matches one space, one tab, or the empty string) and the existing (bad) algorithm find a lot of nothing in the input string... forever. In other words, you hit a bug in that regex implementation.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
0

The code is fine. The regex library is mostly not implemented in your version of libc++. Your best bet is to use another library like boost or an updated version of libc++.

Hna
  • 1,006
  • 9
  • 16
0

I downloaded and compiled the bleeding edge version of libc++ and the "^( |\t)*" version no longer blocks. So I am going to chalk this up to an old library.

Andrew Hoos
  • 1,510
  • 2
  • 15
  • 23