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

If !objMatches.Count Then Return strString

strList = ""

intM = 0
intPos = 1
objMatch = objMatches.item(intM)
strList = strList : strDelimiter : StrSub (strString, intPos, objMatch.FirstIndex)
intPos = 1 + objMatch.FirstIndex + objMatch.Length
ForEach objSubMatch In objMatch.SubMatches
   If objSubMatch != "" Then strList = strList : strDelimiter : objSubMatch
Next

intM = intM + 1
While intM < objMatches.Count
   objMatch = objMatches.item(intM)
   strList = strList : strDelimiter : StrSub (strString, intPos, objMatch.FirstIndex - intPos + 1)
   intPos = 1 + objMatch.FirstIndex + objMatch.Length
   ForEach objSubMatch In objMatch.SubMatches
      If objSubMatch != "" Then strList = strList : strDelimiter : objSubMatch
   Next
   intM = intM + 1
EndWhile

strList = strList : strDelimiter : StrSub (strString, intPos, -1)

If !!intMode
   objRegExp.Pattern = "^" : strDelimiter : "+|" : strDelimiter : "+$" ; Remove all leading and trailing delimiters.
   Return "" : objRegExp.Replace(strList, "")
Else
   Return StrSub (strList, 2, -1) ; Remove leading anchor delimiter.
EndIf

;..........................................................................................................................................
; This UDF udfStrRegExpSplitList 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 string list of matches and possibly submatches.
;
; intMode = 0 : Leave list as is, do not trim the list.
; intMode = 1 : Trim the list (remove all empty items at the bounds).
;
; 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.

strDelimiter = @LF

:Test1
strString1 = "plum-pear"
strRegExpPattern1 = "-"
strSplitList1 = udfStrRegExpSplitList (strString1, strRegExpPattern1, strDelimiter, 0)
;   "plum"
;   "pear"
BreakPoint

strString2 = "plum-pear"
strRegExpPattern2 = "(-)"
strSplitList2 = udfStrRegExpSplitList (strString2, strRegExpPattern2, strDelimiter, 0)
;   "plum"
;   "-"
;   "pear"
BreakPoint

:Test3
strString3 = "-plum--pear-"
strRegExpPattern3 = "-"
strSplitList3 = udfStrRegExpSplitList (strString3, strRegExpPattern3, strDelimiter, 0)
;   ""
;   "plum"
;   ""
;   "pear"
;   ""
BreakPoint

:Test4
strString4 = "07/14/2007"
strRegExpPattern4 = "(-)|(/)"
strSplitList4 = udfStrRegExpSplitList (strString4, strRegExpPattern4, strDelimiter, 0)
;   "07"
;   "/"
;   "14"
;   "/"
;   "2007"
BreakPoint

:Test5
strString5 = "characters"
strRegExpPattern5 = ""
strSplitList5 = udfStrRegExpSplitList (strString5, strRegExpPattern5, strDelimiter, 1)
; intMode=0      intMode=1
;   ""             "c"
;   "c"            "h"
;   "h"            "a"
;   "a"            "r"
;   "r"            "a"
;   "a"            "c"
;   "c"            "t"
;   "t"            "e"
;   "e"            "r"
;   "r"            "s"
;   "s"
;   ""
BreakPoint

:Test6
strString6 = "characters"
strRegExpPattern6 = "#"
strSplitList6 = udfStrRegExpSplitList (strString6, strRegExpPattern6, strDelimiter, 0)
;   "characters"
BreakPoint

:Test7
strString7 = ""
strRegExpPattern7 = ""
strSplitList7 = udfStrRegExpSplitList (strString7, strRegExpPattern7, strDelimiter, 1)
; intMode=0      intMode=1
;   ""             ""
;   ""
BreakPoint

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

Exit