Hello, I'm using boost's regex for a school project (they encourage us
to use boost) Boost 1.34 is what is available on the school computers
(unless fedora 9 promotes a newer version out of testing before Tuesday.)
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
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
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
participants (2)
-
Jeff Anderson
-
Steven Watanabe