我们有一些经典的asp站点,我正在研究它们,我想知道如何编写正则表达式检查,并提取匹配的表达式:
我所拥有的表达式是脚本名称,所以我们这样说吧
Response.Write Request.ServerVariables("SCRIPT_NAME")
Run Code Online (Sandbox Code Playgroud)
打印出来:
review_blabla.asp
review_foo.asp
review_bar.asp
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得的blabla,foo并bar从那里?
谢谢.
Ric*_*son 19
虽然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
Run Code Online (Sandbox Code Playgroud)
另外,我还没有找到比Regexr更好的RegEx游乐场,在深入研究代码之前尝试你的正则表达式模式是很棒的.
您必须使用匹配对象中的子匹配集合来从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
Run Code Online (Sandbox Code Playgroud)