Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using classic asp for regular expression

We have some Classic asp sites, and i'm working on them a lil' bit, and I was wondering how can I write a regular expression check, and extract the matched expression:

the expression I have is in the script's name so Let's say this

Response.Write Request.ServerVariables("SCRIPT_NAME")

Prints out:

review_blabla.asp
review_foo.asp
review_bar.asp

How can I get the blabla, foo and bar from there?

Thanks.

like image 582
Itai Sagi Avatar asked Jul 13 '11 08:07

Itai Sagi


2 Answers

Whilst Yots' answer is almost certainly correct, you can achieve the result you are looking for with a lot less code and somewhat more clearly:

'A handy function i keep lying around for RegEx matches'
Function RegExResults(strTarget, strPattern)

    Set regEx = New RegExp
    regEx.Pattern = strPattern
    regEx.Global = true
    Set RegExResults = regEx.Execute(strTarget)
    Set regEx = Nothing

End Function

'Pass the original string and pattern into the function and get a collection object back'
Set arrResults = RegExResults(Request.ServerVariables("SCRIPT_NAME"), "review_(.*?)\.asp")

'In your pattern the answer is the first group, so all you need is'
For each result in arrResults
    Response.Write(result.Submatches(0))
Next

Set arrResults = Nothing

Additionally, I have yet to find a better RegEx playground than Regexr, it's brilliant for trying out your regex patterns before diving into code.

like image 125
Richard Benson Avatar answered Sep 22 '22 05:09

Richard Benson


You have to use the Submatches Collection from the Match Object to get your data out of the review_(.*?)\.asp Pattern

Function getScriptNamePart(scriptname)
    dim RegEx : Set RegEx = New RegExp
    dim result : result = ""
    With RegEx
        .Pattern = "review_(.*?)\.asp"
        .IgnoreCase = True
        .Global = True
    End With

    Dim Match, Submatch
    dim Matches : Set Matches = RegEx.Execute(scriptname)    
    dim SubMatches
    For Each Match in Matches
        For Each Submatch in Match.SubMatches
                result = Submatch
        Exit For
        Next
    Exit For
    Next

    Set Matches = Nothing
    Set SubMatches = Nothing
    Set Match = Nothing
    Set RegEx = Nothing

    getScriptNamePart = result
End Function
like image 34
Yots Avatar answered Sep 24 '22 05:09

Yots