
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<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 "/". I've played with different arguments to the boost::regex constructor, but I can't seem to get it to output a correct result. I did notice that in the notes for the boost regex library in 1.34 that there was a bug fixed related to non-greedy quantifiers, so I'm fairly confident that I just have a problem with my own code. A tarball can be provided with a "ready-to-compile" example snippet and test case. Then again, it could just as likely be a problem with the regular expression itself, though I checked, re-checked, and double checked that this is the correct syntax for the perl mode in the boost regex library. Thanks for any input! Jeff Anderson

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
participants (2)
-
Jeff Anderson
-
Steven Watanabe