使用经典的asp进行正则表达式

时间:2022-11-26 01:35:11

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:

我们有一些经典的asp站点,我正在研究它们,我想知道如何写一个正则表达式检查,并提取匹配的表达式:

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?

如何从那里得到blabla foo bar ?

Thanks.

谢谢。

4 个解决方案

#1


18  

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:

虽然Yots的答案几乎肯定是正确的,但是您可以用更少的代码和更清晰的代码来实现您想要的结果:

'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.

此外,我还没有找到一个比RegEx更棒的RegEx游乐场,在深入到代码之前尝试您的RegEx模式是非常棒的。

#2


3  

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

您必须使用来自Match对象的子匹配集合从review_(.*?)\获取数据。asp模式

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

#3


0  

You can do

你可以做

review_(.*?)\.asp

See it here on Regexr

在Regexr上可以看到

You will then find your result in capture group 1.

然后,您将在捕获组1中找到您的结果。

#4


0  

You can use RegExp object to do so. Your code gonna be like this:

您可以使用RegExp对象来这样做。你的代码是这样的:

Set RegularExpressionObject = New RegExp
RegularExpressionObject.Pattern = "review_(.*)\.asp"
matches = RegularExpressionObject.Execute("review_blabla.asp")

Sorry, I can't test code below right now. Check out usage at MSDN http://msdn.microsoft.com/en-us/library/ms974570.aspx

对不起,我现在不能测试下面的代码。查看MSDN http://msdn.microsoft.com/en-us/library/ms974570.aspx的使用情况。

#1


18  

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:

虽然Yots的答案几乎肯定是正确的,但是您可以用更少的代码和更清晰的代码来实现您想要的结果:

'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.

此外,我还没有找到一个比RegEx更棒的RegEx游乐场,在深入到代码之前尝试您的RegEx模式是非常棒的。

#2


3  

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

您必须使用来自Match对象的子匹配集合从review_(.*?)\获取数据。asp模式

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

#3


0  

You can do

你可以做

review_(.*?)\.asp

See it here on Regexr

在Regexr上可以看到

You will then find your result in capture group 1.

然后,您将在捕获组1中找到您的结果。

#4


0  

You can use RegExp object to do so. Your code gonna be like this:

您可以使用RegExp对象来这样做。你的代码是这样的:

Set RegularExpressionObject = New RegExp
RegularExpressionObject.Pattern = "review_(.*)\.asp"
matches = RegularExpressionObject.Execute("review_blabla.asp")

Sorry, I can't test code below right now. Check out usage at MSDN http://msdn.microsoft.com/en-us/library/ms974570.aspx

对不起,我现在不能测试下面的代码。查看MSDN http://msdn.microsoft.com/en-us/library/ms974570.aspx的使用情况。