Opened 17 years ago

Closed 10 years ago

Last modified 10 years ago

#589 closed Bugs (fixed)

regex_replace bug

Reported by: nobody Owned by: John Maddock
Milestone: Component: regex
Version: None Severity: Problem
Keywords: Cc:

Description (last modified by John Maddock)

boost::tregex e("(.*)");
strResult = boost::regex_replace("a", e, "$1b");

strResult now equals "abb", but should be "ab".

My e-mail: kdn01003@student.mdh.se

Change History (7)

comment:1 by John Maddock, 17 years ago

Logged In: YES 
user_id=14804

This is by design, and is compatible with the way Perl does
things: two matches are found, the first matches the "a",
the second matches the zero length string immediately after
the "a".  Therefore two replacements are performed, and
hense the output.  Use .+ instead of .* if you don't want to
find zero length matches.

John Maddock

comment:2 by John Maddock, 17 years ago

Status: assignedclosed

comment:3 by coffman@…, 11 years ago

Severity: Problem

I have found this behavior still exists even when using posix basic or extended regexes. While this behavior may be correct for perl regexes it doesn't seem to be correct for posix basic or extended regexes.

std::string s("a"); const boost::regex e("(.*)", boost::regex::extended); std::string result = boost::regex_replace(s, e, "$&b");

The result is "abb"

If I use gnu's sed utility (GNU sed version 4.1.2) instead I get "ab"

echo a | sed -r "s/(.*)/\1b/g"

and gnu's sed utility claims to use posix extended regex expressions when called with the -r option.

comment:4 by coffman@…, 11 years ago

Resolution: Invalid
Status: closedreopened

comment:5 by John Maddock, 10 years ago

Resolution: fixed
Status: reopenedclosed

(In [79339]) Fix regex_iterator and regex_replace to do what grep/sed do for multiple repeats when matching in POSIX mode. Fixes #589.

comment:6 by John Maddock, 10 years ago

Description: modified (diff)

This is now fixed in Trunk provided match_posix is passed to regex_replace or regex_iterator.

comment:7 by John Maddock, 10 years ago

(In [79556]) Merge collected bug fixes from Trunk: Refs #589. Refs #7032. Refs #7084. Refs #6346.

Note: See TracTickets for help on using tickets.