Method RegEx():replaceCallback() Foundation

Replaces matches using a callback code block.

Syntax
:replaceCallback( <cSubject>, <bCallback>, [<lGlobal>] ) --> cString
Parameters
<cSubject>
The string to process.
<bCallback>
A callback code block which is called for each match. The code block receives an array with the match results as the first parameter. The format of the array corresponds to the array returned by the method :match(). See there for a detailed description. Also see the helper methods :getMatchText(), :getMatchPos() and :getMatchRange() which simplify the processing of the match results.
<lGlobal>
Determines whether all or only the first match is replaced. If set to .T. (true), all matches are replaced. Defaults to .F. (false), meaning that the operation terminates after the first match.
Return

Modified string with replacements.

Description

Performs search and replace operations where the replacement text is determined by a callback code block. The callback receives the full match information as a parameter and must return a suitable replacement string.

Replace callback

oRegEx := RegEx():create("\d+") 

// Double each number 
cResult := oRegEx:replaceCallback("10 and 20", ; 
{|aMatch| Var2Char(Val(RegEx():getMatchText(aMatch)) * 2)}, .T.) 
? cResult  // "20 and 40" 

oRegEx:destroy() 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.