Regex Bug in 1.33.1

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Regex Bug in 1.33.1

Olaf Bachmann
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?

Thanks,
Olaf


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

smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Regex Bug in 1.33.1

Eric Niebler

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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users