udfStrRegExpSplit
arr udfStrRegExpSplit (str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrRegExpSplit (strString, strRegExpPattern)
objRegExp = ObjectCreate ("VBScript.RegExp")
objRegExp.Pattern = strRegExpPattern
objRegExp.IgnoreCase = @FALSE
objRegExp.Global = @TRUE
objRegExp.MultiLine = @FALSE
objMatches = objRegExp.Execute(strString)

If !objMatches.Count
   arrA = ArrDimension (1)
   arrA [0] = strString
   Return arrA
EndIf

intA = 0
ForEach objMatch In objMatches
   intA = intA + 1
   ForEach objSubMatch In objMatch.SubMatches
      If objSubMatch != "" Then intA = intA + 1
   Next
Next
arrA = ArrDimension (intA + 1)

intM = 0
intA = 0
intPos = 1
objMatch = objMatches.item(intM)
arrA [intA] = StrSub (strString, intPos, objMatch.FirstIndex)
intPos = 1 + objMatch.FirstIndex + objMatch.Length
ForEach objSubMatch In objMatch.SubMatches
   If objSubMatch != ""
      intA = intA + 1
      arrA [intA] = objSubMatch
   EndIf
Next

intM = intM + 1
intA = intA + 1
While intM < objMatches.Count
   objMatch = objMatches.item(intM)
   arrA [intA] = StrSub (strString, intPos, objMatch.FirstIndex - intPos + 1)
   intPos = 1 + objMatch.FirstIndex + objMatch.Length
   ForEach objSubMatch In objMatch.SubMatches
      If objSubMatch != ""
         intA = intA + 1
         arrA [intA] = objSubMatch
      EndIf
   Next
   intM = intM + 1
   intA = intA + 1
EndWhile

arrA [intA] = StrSub (strString, intPos, -1)

Return arrA
;..........................................................................................................................................
; This UDF udfStrRegExpSplit mimics the behaviour of the .NET Framework Class Library Regex.Split Method (String, String).
; This function splits the specified input string (strString) at the positions
; defined by a regular expression pattern (strRegExpPattern).
; The result is a dim-1 array of matches and possibly submatches.
;
; Detlev Dalitz.20090628.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

If Param0 Then If Param1 == ".." Then Return ; Leave this script here when called by another script with the specified parameter.


; Test.

:Test1
strString1 = "plum-pear"
strRegExpPattern1 = "-"
arrA1 = udfStrRegExpSplit (strString1, strRegExpPattern1)
;   "plum"
;   "pear"
BreakPoint

strString2 = "plum-pear"
strRegExpPattern2 = "(-)"
arrA2 = udfStrRegExpSplit (strString2, strRegExpPattern2)
;   "plum"
;   "-"
;   "pear"
BreakPoint

:Test3
strString3 = "-plum--pear-"
strRegExpPattern3 = "-"
arrA3 = udfStrRegExpSplit (strString3, strRegExpPattern3)
;   ""
;   "plum"
;   ""
;   "pear"
;   ""
BreakPoint

:Test4
strString4 = "07/14/2007"
strRegExpPattern4 = "(-)|(/)"
arrA4 = udfStrRegExpSplit (strString4, strRegExpPattern4)
;   "07"
;   "/"
;   "14"
;   "/"
;   "2007"
BreakPoint

:Test5
strString5 = "characters"
strRegExpPattern5 = ""
arrA5 = udfStrRegExpSplit (strString5, strRegExpPattern5)
;   ""
;   "c"
;   "h"
;   "a"
;   "r"
;   "a"
;   "c"
;   "t"
;   "e"
;   "r"
;   "s"
;   ""
BreakPoint

:Test6
strString6 = "characters"
strRegExpPattern6 = "#"
arrA6 = udfStrRegExpSplit (strString6, strRegExpPattern6)
;   "characters"
BreakPoint

:Test7
strString7 = ""
strRegExpPattern7 = ""
arrA7 = udfStrRegExpSplit (strString7, strRegExpPattern7)
;   ""
;   ""
BreakPoint

:Test8
strString8 = "http://msdn.microsoft.com:80/scripting/default.htm"
strRegExpPattern8 = "[:/]+"
arrA8 = udfStrRegExpSplit (strString8, strRegExpPattern8)
;   "http"
;   "msdn.microsoft.com"
;   "80"
;   "scripting"
;   "default.htm"
BreakPoint

Exit