使用经典的 asp 进行正则表达式 [英] using classic asp for regular expression
问题描述
我们有一些经典的 asp 网站,我正在研究它们,我想知道如何编写正则表达式检查并提取匹配的表达式:
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")
打印出来:
review_blabla.asp
review_foo.asp
review_bar.asp
如何从那里获取 blabla
、foo
和 bar
?
How can I get the blabla
, foo
and bar
from there?
谢谢.
推荐答案
虽然 Yots 的答案几乎可以肯定是正确的,但您可以用更少的代码和更清晰的方式实现您正在寻找的结果:
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
此外,我还没有找到比 Regexr 更好的 RegEx 游乐场,之前尝试您的 regex 模式非常棒深入研究代码.
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.
这篇关于使用经典的 asp 进行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!