Skip to content

SubstituteExCallback - cRegEx

Replaces all matches in a string with result of a function

Type: Function
Return Data Type: String

Parameters

Parameter Type Description
sSubject String The host string to run the regular expression on
hoObj Handle Handle of the cRegEx object
hoMessage Handle Handle of the procedure to call as callback method

Syntax

Function SubstituteExCallback String sSubject Handle hoObj Handle hoMessage Returns String

Call Example

Get SubstituteExCallback sSubject hoObj hoMessage to StringVariable

Description

Replaces all matches in a string with result of a function.

Calls the passed function for each match in the subject string and replaces the match with the return value of that function. If the pattern contains groups, it will call the function for each group but not for the full match, if the pattern does not contain groups it will call the function for the full matches. Parameters to the callback function are the group name ("full_match" for a full match, group name or number for a group match), match value, start offset and end offset.

Signature of Callback Function:

Function SubstituteCallack String sName String sValue Integer iStartOffset Integer iEndOffset Returns String

Sample

In the sample below sResult will end up containing "This is no ! This is *!".

Object oRedacter is a cRegEx 
    Set psExpression to "fun|luck" 
    Set pbIgnoreCase to True 

    Function OnSubstituteCallack String sName String sValue Integer iStartOffset Integer iEndOffset Returns String 
        Function_Return (Repeat("*", iEndOffset - iStartOffset)) 
    End_Function 

    Function Redact String sSubject Returns String 
        Get SubstituteExCallback sSubject Self (RefFunc(OnSubstituteCallack)) to sSubject 
        Function_Return sSubject 
    End_Function 
End_Object 

String sResult 
Get Redact of oRedacter "This is no fun! This is luck!" to sResult

Return Value

String with substitutions for matches