Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost-regex: Weird behaviour with non-greedy matching operator in regex_replace in boost 1.40?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-09-23 11:58:11


AMDG

Florian Schwarz wrote:
> the non-greedy matching seems to have a weird behaviour (maybe
> non-determeterministic?) to work if the pattern is preceeded with
> something.

Boost.Regex doesn't guarantee that it finds the longest possible match.
perl does exactly the same thing:

$ perl -e '$x = "hhallo"; $x =~ s/h(.*?)o?/$1/g; print $x;'
allo

> E.g. when I want to get all characters in a string except the last on,
> if its an 'o', I would use
> regex matchExpr("(.*?)o?");
> So if I write
> string text("hallo");
> regex matchExpr("(.*?)o?");
> string valueExpr("$1");
> string result;
> regex_replace(back_inserter(resul), text.begin(), text.end(),
> matchExpr, valueExpr);
> cout << "Match \"" << result << "\"" << endl;
> it will print the expected "hall". If I now use instead
> string text(" hallo");
> regex matchExpr(" (.*?)o?");
> it will print "hallo". And for
> string text("hhallo");
> regex matchExpr("h(.*?)o?");
> string valueExpr("$1");
> regex_replace(back_inserter(resul), text.begin(), text.end(),
> matchExpr, valueExpr);
> cout << "Match \"" << result << "\"" << endl;
> it will print "allo" which seems even more strange to me.

In this case, the regex matches each 'h'.

In Christ,
Steven Watanabe


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