Syntax: FoundCount := RegExMatchArray(Haystack, NeedleRegEx, OutputArray[, StartingPosition = 1, SubPats = 1])
Information for Haystack, NeedleRegEx and StartingPosition can be found in the documentation for RegExMatch. Most of the rest of this can be too, but I felt like editing it a little to fit the function (also, I was bored). FoundCount:
RegExMatchArray returns the number of times NeedleRegEx could be matched in Haystack. If an error occurs, a blank string is returned and ErrorLevel is set to one of the values here. This value will also be stored in OutputArray0OutputArray:
The base name of the pseudo-array that will contain all of the pieces of Haystack matched by RegExMatchArray. If the pattern is not found, the array will not be created. Note that the array name MUST be enclosed in quotes.
Mode 1 (default): If any capturing subpatterns are found, they will be stored in an array whose base name is OutputArrayMatch#. For example, if the array's name is Match, and we're currently working on the first complete match of NeedleRegEx in Haystack, the substring that matches the first subpattern would be stored in Match1_1, the second in Match1_2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern Year would be stored in Match#_Year. If a particular subpattern does not match anything, it's variable is made blank. The variable OutputArray0_0 will contain the number of subpatterns that could be matched.
Mode 2 (position-and-length): See RegExMatch documentation for more information. Position and length of subpatterns are stored in Match#_Len# and Match#_Pos#.SubPats:
This flag tells the function whether or not to create the secondary arrays for subpatterns (ie. Match#_#). Default is 1.[/list]
And finally, here is the function. Please tell me if anything doesn't work as expected
RegExMatchArray(Haystack, NeedleRegEx, OutputArray, StartingPosition=1, SubPats=1) { Global Local Pos := StartingPosition - 1, Sub := 0, Idx := 0, Mode := 1, SPats, Str If (SubPats) { Loop { If (!Sub := RegExMatch(NeedleRegEx, "(?<!\\)\((?!\?:)", Str, Sub + 1)) Break RegExMatch(NeedleRegEx, "\(\?\<[^\>]+\>", Str, Sub) SPats .= (SPats ? "|" : "") . (Str ? SubStr(Str,4,-1) : A_Index) } } If (RegExMatch(NeedleRegEx, "^[^\)]+\)", Str)) Mode := InStr(Str, "P", 1) ? 2 : 1 Loop { If (!Pos := RegExMatch(Haystack, NeedleRegEx, Str, Pos + 1)) Break IfEqual, 0, % !ErrorLevel, Break Idx := A_Index %OutputArray%%Idx% := Str %OutputArray%0 := Idx Loop, Parse, SPats, | { If (Mode = 1) %OutputArray%%Idx%_%A_LoopField% := Str%A_LoopField% Else { %OutputArray%%Idx%_Len%A_LoopField% := StrLen%A_LoopField% %OutputArray%%Idx%_Pos%A_LoopField% := StrPos%A_LoopField% } %OutputArray%0_0 := A_Index } } IfEqual, 0, % !ErrorLevel, Return Return Idx }