Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2006-04-02 20:49:08


Kyle Alons wrote:
> std::string in("hello");
> std::string exprStr("(.*)");
> std::string replStr("goodbye$1");
>
> boost::regex expr(exprStr);
> std::string out(boost::regex_replace(in, expr, replStr));
>
> printf("%s", out.c_str());
>
> In Boost 1.31, this outputs "goodbyehello".
>
> In 1.32+, it outputs "goodbyehellogoodbye".
>
> Is it a bug, a by-design breaking change, or? Thanks.

By design. The old behavior was buggy. By default, regex_replace
substitutes globally, and your pattern matches the string in two places:
first, it matches the whole string; second, it matches the empty
sequence at the end. In perl:

$str = 'hello';
$str =~ s/(.*)/goodbye$1/g;
print "$str\n";

This prints "goodbyehellogoodbye". Have a look in the docs for
regex_constants::format_first_only.

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net