https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64140
Bug ID: 64140 Summary: match_results.prefix() returns an incorrect result if regex_iterator holds a zero-length match Product: gcc Version: 5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: kariya_mitsuru at hotmail dot com Created attachment 34156 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34156&action=edit g++ -v Please see the following sample. ========================================== sample code ========================================== #include <iostream> #include <regex> #include <string> void print(const char* t, const std::string& s, const std::ssub_match& sub) { std::cout << " " << t << ": " << (sub.matched ? "matched " : "unmatched") << ", " "length() = " << sub.length() << ", str() = '" << sub.str() << "\', " "pair = (" << sub.first - s.begin() << ", " << sub.second - s.begin() << "), " "'" << std::string(sub.first, sub.second) << '\'' << std::endl; } int main() { const std::regex e("z*"); const std::string s("ab"); int i = 0; for (auto&& it = std::sregex_iterator(s.begin(), s.end(), e), end = std::sregex_iterator(); it != end; ++it) { std::cout << i++ << ':' << std::endl; print("prefix", s, it->prefix()); print("match ", s, (*it)[0]); std::cout << std::endl; } } ================================================================================================= ============================= output ============================= 0: prefix: unmatched, length() = 0, str() = '', pair = (0, 0), '' match : matched , length() = 0, str() = '', pair = (0, 0), '' 1: prefix: unmatched, length() = 0, str() = '', pair = (0, 1), 'a' match : matched , length() = 0, str() = '', pair = (1, 1), '' 2: prefix: unmatched, length() = 0, str() = '', pair = (1, 2), 'b' match : matched , length() = 0, str() = '', pair = (2, 2), '' ================================================================== cf. http://melpon.org/wandbox/permlink/JSkP6tl2QWFxmOEv According to C++11 standard 28.11.3[re.alg.search]/p.3 Table 143, prefix().matched should be true if prefix().first != prefix().second. (prefix().first is correct, because 28.12.1.4[re.regiter.incr]/p.5 says "match.prefix().first shall be equal to the previous value of match[0].second".) So, I think that the output should be ============================= output ============================= 0: prefix: unmatched, length() = 0, str() = '', pair = (0, 0), '' match : matched , length() = 0, str() = '', pair = (0, 0), '' 1: prefix: matched , length() = 1, str() = 'a', pair = (0, 1), 'a' match : matched , length() = 0, str() = '', pair = (1, 1), '' 2: prefix: matched , length() = 1, str() = 'b', pair = (1, 2), 'b' match : matched , length() = 0, str() = '', pair = (2, 2), '' ==================================================================