/* ---------------------------------------------------------------------------------------------- Title: RegEx *Regular Expression Callbacks* */ /* ---------------------------------------------------------------------------------------------- Function: RegEx_Match RegExMatch with callback Parameters: HayStack - Reference to the string to be analysed Pattern - Regular Expression pattern without named sub-patterns Fun - AHK callback. Function must have number of arguments equal to number of sub-patterns + 1. First parameter contains entire match, additional parameters are values of sub-patterns. Function returns true/false to continue/stop matching. Start - Start position Returns: Number of times callback is called */ RegEx_Match(ByRef HayStack, Pattern, Fun, Start=1){ subPatterns := RegEx_sp(Pattern) i := Start, n :=0 loop, { j := RegExMatch(HayStack, Pattern, o, i), n++ IfEqual, j, 0, return n i := j + strlen(o) goto RegEx_Match%subPatterns% RegEx_Match0: r := %Fun%(o) goto RegEx_Match RegEx_Match1: r := %Fun%(o, o1) goto RegEx_Match RegEx_Match2: r := %Fun%(o, o1, o2) goto RegEx_Match RegEx_Match3: r := %Fun%(o, o1, o2, o3) goto RegEx_Match RegEx_Match4: r := %Fun%(o, o1, o2, o3, o4) goto RegEx_Match RegEx_Match5: r := %Fun%(o, o1, o2, o3, o4, o5) RegEx_Match: ifEqual, r, 0, return n } } /* ---------------------------------------------------------------------------------------------- Function: RegEx_Replace RegExReplace with callback Parameters: HayStack - Reference to the string to be analysed Pattern - Regular Expression pattern without named sub-patterns Fun - AHK callback. Function must have number of arguments equal to number of sub-patterns + 1. First parameter contains entire match, additional parameters are values of sub-patterns. Function returns string that will be used as replacement for current match. Start - Start position Returns: New haystack */ RegEx_Replace(ByRef HayStack, Pattern, Fun, Start=1){ i := Start subPatterns := RegEx_sp(Pattern) loop, { j := RegExMatch(HayStack, Pattern, o, i) ifEqual, j, 0, return result .= SubStr(HayStack, i) goto RegEx_Replace%subPatterns% RegEx_Replace0: rep := %Fun%(o) goto RegEx_Replace RegEx_Replace1: rep := %Fun%(o, o1) goto RegEx_Replace RegEx_Replace2: rep := %Fun%(o, o1, o2) goto RegEx_Replace RegEx_Replace3: rep := %Fun%(o, o1, o2, o3) goto RegEx_Replace RegEx_Replace4: rep := %Fun%(o, o1, o2, o3, o4) goto RegEx_Replace RegEx_Replace5: rep := %Fun%(o, o1, o2, o3, o4, o5) RegEx_Replace: result .= SubStr(HayStack, i, j-i) rep i := j + strlen(o) } return result } ; Private function to calculate number of sub-patterns in the valid expression. RegEx_sp(Pattern){ loop, parse, Pattern { if (A_LoopField = "[") and (p !="\") in_range := true if (A_LoopField = "]") and (p != "\") in_range := false if (A_LoopField = "(") and (p != "\") and !in_range n++ p := A_LoopField } return n } /* ------------------------------------------------------------------------------------------------------------------- Group: Example (start code) text := "a b c abc123efg abc9000cde" s := RegEx_Replace(text, "abc([0-9]+)", "MyFun") msgbox %s% return MyFun($, $1){ return $1 * 2 } (end code) */ ;------------------------------------------------------------------------------------------------------------------- ;Group: About ; o Ver 0.1 by majkinetor. ; o Licenced under Creative Commons Attribution-Noncommercial .