VBScript preg_match function

A VBScript equivalent of PHP’s preg_match


Const PREG_PATTERN_ORDER  = 1
Const PREG_SET_ORDER      = 2
Const PREG_OFFSET_CAPTURE = 256
Function preg_match(pattern, ByVal subject, ByRef matches, flags, offset)

    Dim matchAll,matchone
    Dim cnt,helper

    preg_match = false

    set helper = new RegExp_Helper
    helper.parseOption(pattern)

    Set matchAll = new RegExp
    With matchAll
        .IgnoreCase = helper.withIgnoreCase
        .Global     = false
        .pattern    = helper.withPattern
        .MultiLine  = helper.withMultiLine
    End With

    set helper = Nothing

    If not is_empty(offset) Then
        offset = int(offset)
        subject = Mid(subject,offset)
    End If

    offset = intval( offset )

    If vartype(matches) <> 8 Then
        Set matchone = matchAll.execute(subject)
        Set matchAll = Nothing
        If matchone.count = 0 Then exit Function

        If flags = PREG_OFFSET_CAPTURE Then

            ReDim matches(1)
            matches(0) = matchone(0).value
            matches(1) = offset + matchone(0).FirstIndex
        Else
            ReDim matches(0)
            matches(0) = matchone(0).value
        End If

        preg_match = true
    Else
        preg_match = matchAll.Test(subject)
        Set matchAll = Nothing
    End If

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