|
Boost Users : |
From: John Maddock (john_at_[hidden])
Date: 2006-11-22 04:50:28
Scott Meyers wrote:
> If I want to replace all the regex matches in a string, no problem,
> regex_replace is just what I'm looking for. But suppose I want to
> replace each match only if some predicate is satisfied, e.g., to
> implement something like a string replacement in a text editor where
> the user is allowed to approve or reject each proposed replacement.
> Is there any direct support for something like this?
>
> The only way I can think of to do this is to iterate over the string
> to be searched, finding each match and then consulting the predicate
> to see if the replacement should be performed for that match. If so,
> I can call regex_replace on the found substring, but then I'll get
> back a new string, and I'll have to manually handle the process of
> knitting together the final revised string from the original string
> and the results of each substitution. This is essentially what
> regex_replace already does, except it unconditionally does a
> substitution for each match.
>
> What it seems I want is something like regex_replace_if. Is there
> such a thing in Boost or TR1? If not, is it because there is little
> demand for such functionality? That would surprise me, because
> pretty much every text editor allows one to iteratively search for a
> match and query the user to see if a replacement should be performed
> at that point.
There's no preformed regex_replace_if, but you could probably synthesise one
easily enough. There's no need to call regex_replace to create the new
string BTW: you can call match_results::format to get the string to
substitute in, and then string::replace to insert it. No wait, that's
almost certainly dumb. Probably better to build up a new string (like
regex_replace does actually) and just append to the end of it at each step.
HTH, John.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net