Opened 12 years ago

Closed 6 years ago

Last modified 6 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:

Attachments (0)

Change History (7)

comment:1 Changed 12 years ago by John Maddock

Logged In: YES 

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 Changed 12 years ago by John Maddock

Status: assignedclosed

comment:3 Changed 7 years ago by coffman@…

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 Changed 7 years ago by coffman@…

Resolution: Invalid
Status: closedreopened

comment:5 Changed 6 years ago by John Maddock

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 Changed 6 years ago by John Maddock

Description: modified (diff)

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

comment:7 Changed 6 years ago by John Maddock

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

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain John Maddock.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.