Difference in regex_replace replacement format handling between Boost.Regex and std::regex

Josué Andrade Gomes via Boost-users <[email protected]> Mon, 22 Apr 2024 10:03:44 -0300
Newsgroups gmane.comp.lib.boost.user
Message-ID <CACVaizRgL63qVR7XCxLLhNbMVSQ7Neb=oxd3D6iMAJYKoz43Lg@mail.gmail.com>
There is a difference in how the replacement format string is handled by
regex_replace in Boost.Regex and std::regex.

Consider the following example:

#include <iostream>#include <regex>#include <boost/regex.hpp>
int main() {
    std::string s{"a:b:c"};
    std::string rx{":"};
    std::string r{"\\$&"};

    // Using std::regex
    std::string result_std = std::regex_replace(s, std::regex(rx), r);
    std::cout << "std::regex result: " << result_std << std::endl;  //
Outputs "a\:b\:c"

    // Using Boost.Regex
    std::string result_boost = boost::regex_replace(s, boost::regex(rx), r);
    std::cout << "Boost.Regex result: " << result_boost << std::endl;
// Outputs "a$&b$&c"

    return 0;
}

std::regex result: a\:b\:c
Boost.Regex result: a$&b$&c

Affects Boost.Regex 1.84. I didn’t try other versions.

Tested with GCC 13.2, clang 18.10 , cl 19.39.33523 and results are the
same. Not tested with other versions.

https://github.com/boostorg/regex/issues/210

_______________________________________________
Boost-users mailing list
[email protected]
https://lists.boost.org/mailman/listinfo.cgi/boost-users