🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #78

std::string replace problem with Regex++ and borlands free compiler

#78
From: Jan
Date: Tue, 25 Sep 2001 16:04
76 lines
3570 bytes
Hello group:

I am completely puzzled by this problem. I am trying to execute the
following code for a number of strings. Sometimes the program aborts
with an 'Abnormal program termination' at the string::replace() line
(see code at bottom of mail) and sometimes it works fine.

I am using:
* The regex++ library from
http://ourworld.compuserve.com/homepages/john_maddock/regexpp.htm
* Borland's bcc32 version 5.5.1

My code is at the bottom of this posting. Here are some results:

arg2 = boost::regex("\\d\\d")
arg3 = "rrrrrrrrrrrr"
arg4 = true

arg1                                 result
"ssss1122s"                          abort on second iteration
"sssss1122s"                         success
"ssss11s22s"                         success
"ssss112s"                           success

Well, I have been trying a zillion things by now, but I can't seem to
pin down the problem. Any help is apreciated.

Regards,

Jan.

=============================================================================
Here is my function:

#include <string>
#include <boost/regex.hpp>

int substitute(
	std::string& text, // Text to do substitution(s) in
	const boost::regex& pattern, // Pattern to replace
	const std::string& subst,// String to replace matches of pattern with
	bool global) // false = replace only first match, true = all matches
{
	// Initialize values for search loop
	std::string::iterator start = text.begin();
	std::string::iterator end = text.end();
	boost::match_results<std::string::iterator> what;
	unsigned int flags = boost::match_default;
	int counter = 0;

	while(boost::regex_search(start, end, what, pattern, flags))
	{
           // Do replacement
	   text.replace(what[0].first, what[0].second, subst);
		++counter;

		// If not global discontinue loop
		if(!global) break;

		// Update search start and end position
		start = what[0].second;
		end = text.end(); // string may have a different length after

                // Update flags:
		flags |= boost::match_prev_avail; // indicates that *--first exists
		flags |= boost::match_not_bob;	  // first is not beginning of a
buffer
	}

	return counter; // number of substitutions made
}

==============================================================================

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
Technical Details
Message-ID: <4d706599.0109240839.3e0fb449@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!uwm.edu!rpi!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10c066,e4c94d0a71891692,start
X-Google-Attributes: gid10c066,public
X-Google-Thread: fc772,e4c94d0a71891692,start
X-Google-Attributes: gidfc772,public
X-Google-ArrivalTime: 2001-09-25 09:03:45 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!uwm.edu!rpi!not-for-mail
From: rachmaninof@hotmail.com (Jan)
Newsgroups: comp.lang.c++.moderated,borland.public.cpp.commandlinetools
Subject: std::string replace problem with Regex++ and borlands free compiler
Date: 25 Sep 2001 12:04:03 -0400
Organization: http://groups.google.com/
Lines: 76
Sender: cppmods@netlab.cs.rpi.edu
Approved: hsutter@acm.org
Message-ID: <4d706599.0109240839.3e0fb449@posting.google.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: 24 Sep 2001 09:39:00 -0700
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
 iQBVAwUAO7Cq70HMCo9UcraBAQFewwH6AlDxu+lz9Kf0AMIgl/aNfyw1L+s9pwe9
 KIMH+V4nm7NmtWICergq9Ptj+veKAr9WMpnNDDOPsWhFDHM5gMr+6w==
 =wz/Q