Re: [Boost-users] [regex] Context-sensitive replacement of text usingregex_replace
Hi Ken, what I suggest you do is look at the source for regex_replace: it's actually a pretty simple application of of regex_iterator internally, it should be fairly trivial to modify that to do what you want it to,
HTH, John.
Hi John, Having done something similar a number of times, I wonder if it would be worth implementing a variant of regex_replace where a functor is called that accepts the matched string and returns the replacement string. I think this is a common usage scenario. For example .NET regex's implement this. See http://msdn.microsoft.com/en-us/library/ht1sxswy.aspx Neil Hunt P.S. Thanks for the great library, and getting it into the C++ spec.
Having done something similar a number of times, I wonder if it would be worth implementing a variant of regex_replace where a functor is called that accepts the matched string and returns the replacement string.
I think this is a common usage scenario.
Me too: I was thinking exactly this as I wrote that reply, I'll try and add this for the next release - can I get you to file a feature request at svn.boost.org so I don't forget? Cheers, John.
Having done something similar a number of times, I wonder if it would be worth implementing a variant of regex_replace where a functor is called that accepts the matched string and returns the replacement string.
I think this is a common usage scenario.
Me too: I was thinking exactly this as I wrote that reply, I'll try and add this for the next release - can I get you to file a feature request at svn.boost.org so I don't forget?
find_format_all, in the String_Algo library, with a regex_finder and a custom formatter, can be used for that, IIUC.
John Maddock wrote:
Having done something similar a number of times, I wonder if it would be worth implementing a variant of regex_replace where a functor is called that accepts the matched string and returns the replacement string.
I think this is a common usage scenario.
Me too: I was thinking exactly this as I wrote that reply, I'll try and add this for the next release - can I get you to file a feature request at svn.boost.org so I don't forget?
I don't think such a callback would be useful to the OP. If the callback merely accepts the matched string, it would need to reparse the string to find the sub-matches. In addition, accepting and returning strings imposes unnecessary dynamic allocations. Xpressive's regex_replace() algorithm can accept a formatter function or function object with one of three different signatures. It can either: a) Accept the match_results object, and return a std::string, or b) Accept the match_results object and an output iterator, and return the output iterator, or b) Accept the match_results object, an output iterator and the match flags, and return the output iterator. You can read about it here: http://tinyurl.com/bd6c3f. I like this interface because it gives the formatter object lots of context and makes formatting potentially very efficient. It would be great if Boost.Regex and Boost.Xpressive settled on a common callback interface for regex_replace(). -- Eric Niebler BoostPro Computing http://www.boostpro.com
I don't think such a callback would be useful to the OP. If the callback merely accepts the matched string, it would need to reparse the string to find the sub-matches. In addition, accepting and returning strings imposes unnecessary dynamic allocations.
Xpressive's regex_replace() algorithm can accept a formatter function or function object with one of three different signatures. It can either:
a) Accept the match_results object, and return a std::string, or b) Accept the match_results object and an output iterator, and return the output iterator, or b) Accept the match_results object, an output iterator and the match flags, and return the output iterator.
You can read about it here: http://tinyurl.com/bd6c3f. I like this interface because it gives the formatter object lots of context and makes formatting potentially very efficient.
It would be great if Boost.Regex and Boost.Xpressive settled on a common callback interface for regex_replace().
I think the interface you have there is very good - I'd always assumed that the callback would accept a match_results not a string - as you say the performance would be just so much better... John.
John Maddock wrote:
Eric Niebler wrote:
You can read about it here: http://tinyurl.com/bd6c3f. I like this interface because it gives the formatter object lots of context and makes formatting potentially very efficient.
It would be great if Boost.Regex and Boost.Xpressive settled on a common callback interface for regex_replace().
I think the interface you have there is very good - I'd always assumed that the callback would accept a match_results not a string - as you say the performance would be just so much better...
Great! But if you adopt this interface, folks will have 1 less reason to use xpressive. So I get to keep formatter expressions. ;-) -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (4)
-
Eric MALENFANT
-
Eric Niebler
-
John Maddock
-
Neil Hunt