我们有一些经典的asp网站,我正在研究它们,我想知道如何编写正则表达式检查,并提取匹配的表达式:
我所拥有的表达式是脚本的名称
所以我们这样说吧
Response.Write Request.ServerVariables("SCRIPT_NAME")
打印出来:
review_blabla.asp review_foo.asp review_bar.asp
我怎么能从那里得到blabla,foo和bar?
谢谢.
虽然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,在试用代码之前试用你的正则表达式是很棒的.