Reputation: 1520
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.
Upvotes: 3
Views: 133
Reputation: 1520
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.
Upvotes: 0
Reputation: 1006
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++.
Upvotes: 0
Reputation: 20741
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.
Upvotes: 2