VBScript preg_match_all function

A VBScript equivalent of PHP’s preg_match_all


Function preg_match_all(pattern, ByVal subject, ByRef matches, flags, offset)

    Dim regEx,matchall,matchone
    Dim cnt,counter : counter = 0
    Dim helper

    preg_match_all = false
    If vartype(matches) <> 0 Then Exit Function
    If len(flags) = 0 Then flags = PREG_PATTERN_ORDER

    set helper = new RegExp_Helper
    helper.parseOption(pattern)

    Set regEx = new RegExp
    With regEx
        .IgnoreCase = helper.withIgnoreCase
        .Global     = True
        .pattern    = helper.withPattern
        .MultiLine  = helper.withMultiLine
    End With
    set helper = Nothing

    If len(offset) > 0 Then
        offset = int(offset)
        subject = Mid(subject,offset)
    End If

    Set matchall = regEx.execute(subject)
    Set regEx = Nothing
    If matchall.count = 0 Then exit Function

    If flags = PREG_PATTERN_ORDER Then

        ReDim matches(matchall(0).SubMatches.count)

        For cnt = 0 to uBound(matches)
            toReDim matches(cnt),(matchall.count-1)
        Next

        counter = 0
        For Each matchone In matchall
            matches(0)(counter) = matchone.value
            For cnt = 1 to matchone.SubMatches.count
                matches(cnt)(counter) = matchone.SubMatches(cnt-1)
            Next
            counter = counter + 1
        Next

    Elseif flags = PREG_SET_ORDER Then

        ReDim matches(matchall.count-1)

        counter = 0
        For Each matchone In matchall
            toReDim matches(counter),(matchone.SubMatches.count)
            matches(counter)(0) = matchone.value
            For cnt = 1 to matchone.SubMatches.count
                matches(counter)(cnt) = matchone.SubMatches(cnt-1)
            Next
            counter = counter + 1
        Next

    ElseIf PREG_OFFSET_CAPTURE Then

        ReDim matches(matchall(0).SubMatches.count)

        For cnt = 0 to uBound(matches)
            toReDim matches(cnt),(matchall.count-1)
        Next

        counter = 0
        For Each matchone In matchall
            toReDim matches(0)(counter),1
            matches(0)(counter)(0) = matchone.value
            matches(0)(counter)(1) = matchone.FirstIndex
            For cnt = 1 to matchone.SubMatches.count
                toReDim matches(cnt)(counter),1
                matches(cnt)(counter)(0) = matchone.SubMatches(cnt-1)
                matches(cnt)(counter)(1) = InStr( matchone.value, matchone.SubMatches(cnt-1) ) -1
            Next
            counter = counter + 1
        Next

    End If

    preg_match_all = matchall.Count

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the regexp extension