Boost logo

Boost :

From: John Maddock (john_maddock_at_[hidden])
Date: 2002-05-01 06:07:01

> Just a dumb question, but why does smatch (and cmatch for that matter)
> return const iterators? I'm wanting to do a targetString.replace() with
> the returned iterators from a regex_match( targetString, ... ) at a
> later time, but of course cannot because replace() does not take const
> iterators.
> What are the reason for the const-ness?

'cos a lot of people want to search const-strings, and of course since the
regex lib doesn't alter the string it searches working with const-iterators
is the "right thing to do". IMO the standard is wrong here, it should be
possible to do an erase or replace using const-iterators as markers (I'm far
from the first person to say that however, in fact I think there is a DR on
it somewhere). However none of that helps you does it? There are two

1) Convert the constant iterators into integer indexes (by subtracting
begin()), and use the index as an argument to replace or erase or whatever.
2) There is nothing to stop you from instantiating non-const iterator
versions of the templates, you will need:

typedef boost::match_results<std::string::iterator> mmatch; // or something

and then you will have to call the iterator versions of
regex_match/regex_search rather than the string-overload versions:

mmatch what;
if(regex_match(my_string.begin(), my_string.end(), what, my_regex))
    mystring.replace(what[0].first, what[0].second, my_replace_string);

John Maddock

Boost list run by bdawes at, gregod at, cpdaniel at, john at