|
Boost Users : |
From: Eric Niebler (eric_at_[hidden])
Date: 2006-03-19 09:54:05
Olaf Bachmann wrote:
> Hi,
> there seems to be a bug in version 1.33.1 of boost::regex:
>
> boost::regex_replace(std::string("foo"), boost::regex(".*"), "bar")
> returns "barbar"
>
> whereas 1.31.0 returns just "bar" (as I would expect). Any fix or hints
> out there?
I believe the behavior in 1.33.1 is correct. By default, regex_replace
does a global search/replace. It should behave like the following perl:
$str = 'foo';
$str =~ s/.*/bar/g;
print "$str\n";
This prints "barbar". The reason is that ".*" matches "foo" twice: the
first time it matches "foo" and the second time it matches the
zero-width character sequence after "foo".
If you only want the substitution to happen once, use
match_flag_type::format_first_only, or change your pattern to ".+"
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