RegEx callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching. For detailed information about the PCRE-standard callout feature, see pcre.txt.
RegEx callouts are currently supported only by RegExMatch and RegExReplace.
The syntax for a RegEx callout is (?CNumber:Function), where both Number and Function are optional. Colon ':' is allowed only if Function is specified, and is optional if Number is omitted. A named callout in the pattern must enclose its name in double quotes, single quotes or braces.
A callout function must be a top-level function; closures and nested functions are not supported. If Function is omitted, it defaults to pcre_callout. If no matching function is found, an error is thrown.
MyFunction(Match, CalloutNumber, FoundPos, Haystack, NeedleRegEx)
{
...
}
RegEx callout functions may define up to 5 parameters:
These names are suggestive only. Actual names may vary.
Warning: Changing the input parameters of RegExReplace or RegExMatch during a call is unsupported and may cause unpredictable behaviour.
Pattern-matching may proceed or fail depending on the return value of the RegEx callout function:
For example:
Haystack := "The quick brown fox jumps over the lazy dog."
RegExMatch(Haystack, "i)(The) (\w+)\b(?CCallout)")
Callout(m, *) {
MsgBox "m[0]=" m[0] "`nm[1]=" m[1] "`nm[2]=" m[2]
return 1
}
In the above example, Callout is called once for each substring which matches the part of the pattern preceding the RegEx callout. \b is used to exclude incomplete words in matches such as The quic, The qui, The qu, etc.
If any of the input parameters to a RegEx function is modified during a callout, the behaviour is undefined.
Notice: RegEx callouts do not set A_EventInfo. Use the five callback parameters described above; the native PCRE callout block is not exposed.
Including C in the options of the pattern enables auto-callout mode. Callouts equivalent to (?C255) are inserted before pattern items. The default top-level pcre_callout function receives the five parameters described above.
RegEx callouts execute on the current pseudo-thread.
PCRE is optimized to abort early in some cases if it can determine that a match is not possible. For all RegEx callouts to be called in such cases, it may be necessary to disable these optimizations by specifying (*NO_START_OPT) at the start of the pattern.