Regex recommended usage?

Hi, I have a container (map) with identifiers and value and would like to replace variables in a string. The function to perform the replacement I wrote needs a const_cast, which makes me a bit nervous. Is this necessary? Is there a better way to implement it? Here's the code: // The global variable container; map< string, string > variables; const string resolv_variables( string s ) { // Variables (...${identifier}...) are matched by: static const boost::regex find( "(?:^|.*[^\\\\])(\\$\\{" // start "([a-zA-Z_][a-zA-Z0-9_]*)" // identifier "\\}).*" ); // end boost::smatch what; while( boost::regex_match( s, what, find ) ) { const string::iterator begin = const_cast< string::iterator >( what[ 1 ].first ); const string::iterator end = const_cast< string::iterator >( what[ 1 ].second ); const string identifier = what[ 2 ]; s = s.replace( begin, end, variables[ identifier ] ); } return s; } Regards, Daniel

--- At Tue, 9 Jul 2002 11:11:50 +0200, Daniel Frey wrote:
This is shooting from the hip, but it seems like this might need to be:
const string::iterator begin = const_cast< string::iterator >( what[ 1 ].first );
string::const_iterator begin = what[ 1 ].first; I havent looked at regex to know if this is right. Its just a guess from knowing how iterators work. ( const iterator != const_iterator ) ...Duane -- "If tyranny and oppression come to this land, it will be in the guise of fighting a foreign enemy." - James Madison

You can't portably use const_cast with string::const_iterator, it works in this case because the iterator is just a pointer but that is not true on many platforms (VC7, STLport etc). Better is to use the form of replace that takes an int position and a length: s = s.replace( what[2].first - s.begin(), what.length(2), variables[ identifier ] ); John Maddock http://ourworld.compuserve.com/homepages/john_maddock/index.htm
participants (4)
-
Daniel Frey
-
Daniel Frey
-
Duane Murphy
-
John Maddock