Boost logo

Boost Users :

Subject: Re: [Boost-users] non-greedy regex
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-10-30 15:24:08


AMDG

Jeff Anderson wrote:
> I'm trying to use a non-greedy quantifier in a regex. Here is an example
> from my code:
>
> string input=string("/blog/2008/../");
> string expr=string("/.*?/\\.\\./");
> string replacement=string("/");
> std::ostringstream stream(std::ios::out | std::ios::binary);
> std::ostream_iterator<char, char> stream_iterator(stream);
> boost::regex pattern(expr, boost::regex::perl);
> boost::regex_replace(stream_iterator, input.begin(), input.end(),
> pattern, replacement, boost::match_default | boost::format_all);
>
> string result=stream.str();
>
> The expected output is "/blog/". I am getting "/".
>

So, non-greedy repeats stop as soon as they can. However,
when doing a regex_replace, the library always finds the first
occurrence of the pattern. In other words greediness does not
affect the place where the match begins it only affects how soon the
match ends.

I think that this regex does what you want:

std::string expr("/[^/]*/\\.\\./");

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