Strange (wrong?) behavior of boost::regex_replace()
The program below gives different output in Boost 1.32.0 compared to
1.33.1 and 1.34.1.
In my opinion "R_Test" would be the correct result and therefore Boost
1.33.1 and 1.34.1 would be wrong.
Can anyone clarify this issue?
Best regards, Peter.
#include <iostream>
#include
Peter Klotz wrote:
The program below gives different output in Boost 1.32.0 compared to 1.33.1 and 1.34.1.
In my opinion "R_Test" would be the correct result and therefore Boost 1.33.1 and 1.34.1 would be wrong.
Can anyone clarify this issue?
Best regards, Peter.
#include <iostream> #include
int main() { // Boost 1.32.0: "R_Test" // Boost 1.33.1 and 1.34.1: "R_TestR_" std::cout << boost::regex_replace(std::string("Test"),boost::regex("(.*)"), "R_$1") << std::endl; return 0; }
I would expect the behavior in Boost 1.33+. The regex first matches the full string "Test", then it matches the empty string at the end. Try changing the expression to "(.+)" to match 1 or more characters, or "^(.*)" to match only the text at the beginning of the string.
On Nov 14, 2007 11:26 AM, Andrew Holden
I would expect the behavior in Boost 1.33+. The regex first matches the full string "Test", then it matches the empty string at the end.
Try changing the expression to "(.+)" to match 1 or more characters, or "^(.*)" to match only the text at the beginning of the string.
It seems counter-intuitive to me that consuming an entire string should leave an empty string behind. After all, after that empty string is consumed, there's still another empty string, and another, etc. -Max -- "The presentation or 'gift' of the Holy Ghost simply confers upon a man the right to receive at any time, when he is worthy of it and desires it, the power and light of truth of the Holy Ghost, although he may often be left to his own spirit and judgment." --Joseph F. Smith (manual, p. 69) Be pretty if you are, Be witty if you can, But be cheerful if it kills you.
Maximilian Wilson wrote:
It seems counter-intuitive to me that consuming an entire string should leave an empty string behind. After all, after that empty string is consumed, there's still another empty string, and another, etc.
I agree. However, the following Perl program demonstrates the same behavior, so it isn't just Boost.Regex. Since Boost.Regex attempts to emulate Perl by default, this "bug" is correct. #Begin Perl sample #! /usr/bin/perl $v = 'Test'; $v =~ s/(.*)/R_$1/g; print "$v\n"; #Prints "R_TestR_" #End Perl sample Now that I think about it, didn't Boost.Regex change the default mode from POSIX to Perl between 1.32 and 1.33? Perhaps you can recover the old behavior by constructing the regex object as 'boost::regex ("(.*)", boost::regex::extended)' or 'boost::regex ("(.*)", boost::regex::basic)'. I can't remember which one was the default in boost 1.32. As a side note, please avoid posting HTML messages. They cause havoc with many mail readers.
Andrew Holden wrote:
Maximilian Wilson wrote:
It seems counter-intuitive to me that consuming an entire string should leave an empty string behind. After all, after that empty string is consumed, there's still another empty string, and another, etc.
I agree. However, the following Perl program demonstrates the same behavior, so it isn't just Boost.Regex. Since Boost.Regex attempts to emulate Perl by default, this "bug" is correct.
#Begin Perl sample #! /usr/bin/perl
$v = 'Test'; $v =~ s/(.*)/R_$1/g; print "$v\n"; #Prints "R_TestR_" #End Perl sample
Right, this change was deliberate, and was made in response to a bug report. HTH, John.
participants (4)
-
Andrew Holden
-
John Maddock
-
Maximilian Wilson
-
Peter Klotz