Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2008-04-10 08:53:21


Mihaly Zachar wrote:
> I'm using libboost-regex1.34.1 from gutsy.
>
> here is a sample for my problem:
>
> ------- CUT -------
> #include <boost/regex.hpp>
> #include <string>
> #include <iostream>
>
> using namespace std;
> using namespace boost;
>
> int main()
> {
> string input = "1111";
> string a = "(.*)";
> string b = "0$1";
>
> regex reg (a, regex::perl);
>
> string out = regex_replace(input, reg, b);
>
>
> cout << out << endl;
>
> return 0;
> }
>
> ------- CUT -------
>
>
> the expected result would be "01111" but it gives "011110"...
> why does it work like this ?

By design: Perl does the same thing, after the first match against "1111"
there is a second match against the empty string at the end of the text.

> if I use anchors (^ and $) it works well, but the problem is I will
> not
> get anchors from the other applications :(
>
> the problem is that I get regexes from different perl and php
> applications...
>
> will the upgrade to 1.35 solve my problem ?

Nope, and as I say, Perl does the same thing. You could set the match flag
match_not_null as the last argument to regex_replace, but that would disable
matches against all zero-length strings which may not be what you want.

Was this a real use case, or just a "getting to know the library" test case?

HTH, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk