autohotkey

AHK Populate an Array of Regex Matches


In AHK i'm trying to populate an array of matches in the style of match[i]. Here is what I have so far:

string = "red"
RegExMatch(string, "O)([a-z])", Match)
MsgBox % Match[1] . Match[2] . Match[3]

However, it merely returns r instead of red.

Any help is greatly appreciated.


Solution

  • There is no such thing as "Matches" in RegExMatch(). The documentation says

    returns the position of the leftmost occurrence of NeedleRegEx in the string Haystack

    and "Mode 3 (match object)" (which is what you used) says

    a match object [...] retrieve[s] the position, length and value of the overall match and of each captured subpattern, if present.

    Meaning, the subpattern(s) apply only to the leftmost match. Your expression contains only one subpattern: ([a-z]).

    To catch multiple matches (of the same expression) within the same string, you'll have to build a loop around it and shift the StartingPosition parameter accordingly.