【问题标题】:using classic asp for regular expression使用经典的 asp 进行正则表达式
【发布时间】:2011-07-13 08:09:52
【问题描述】:

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

我的表达式在脚本的名称中 所以让我们这么说

Response.Write Request.ServerVariables("SCRIPT_NAME")

打印出来:

review_blabla.asp
review_foo.asp
review_bar.asp

如何从那里获得blablafoobar

谢谢。

【问题讨论】:

  • 你用的是什么语言,VBscript 还是 JScript?
  • 看起来像 VBScript。上帝怜悯他的灵魂。
  • 这是一个非常古老的网站,很遗憾,我们没有时间重写整个内容

标签: regex asp-classic


【解决方案1】:

虽然 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

此外,我还没有找到更好的RegEx playground than Regexr,在深入研究代码之前尝试您的正则表达式模式非常棒。

【讨论】:

    【解决方案2】:

    您必须使用匹配对象中的子匹配集合来将您的数据从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】:

      你可以的

      review_(.*?)\.asp
      

      here on Regexr

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

      【讨论】:

        【解决方案4】:

        您可以使用 RegExp 对象来执行此操作。 你的代码会是这样的:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多