Thread View: comp.lang.c++.moderated
1 messages
1 total messages
Started by Jan
Tue, 25 Sep 2001 16:04
std::string replace problem with Regex++ and borlands free compiler
Author: Jan
Date: Tue, 25 Sep 2001 16:04
Date: Tue, 25 Sep 2001 16:04
76 lines
3570 bytes
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! ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads