正则表达式-贪婪模式,非贪婪模式(asp版)
贪婪模式在整个表达式匹配成功的前提下,尽可能多的匹配,而非贪婪模式在整个表达式匹配成功的前提下,尽可能少的匹配。
属于贪婪模式的量词,也叫做匹配优先量词,包括:
“{m,n}”、“{m,}”、“?”、“*”和“+”。
在一些使用NFA引擎的语言中,在匹配优先量词后加上“?”,即变成属于非贪婪模式的量词,也叫做忽略优先量词,包括:
“{m,n}?”、“{m,}?”、“??”、“*?”和“+?”。
非贪婪模式 示例:
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全程可用性。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 遍历 Matches 集合。
RetStr = RetStr & Match.FirstIndex & "。匹配的长度为"
RetStr = RetStr & Match.Length &" " & "<br>"
RetStr = RetStr & Match.SubMatches(0) &" "& "<br>"
'RetStr = RetStr & Match.SubMatches(1)&" " & "<br>"
'RetStr = RetStr & Match.SubMatches(2)&" " & "<br>"
RetStr = RetStr & Match.value&" " & "<br>"
RetStr = RetStr & "<br>--------------<br>"
Next
RegExpTest = RetStr
End Function
str="#lee#sdf <b>222</b> dsfse#/lee##lee#我是中国人#/lee#"
pt="#lee#([\s\S]*?)#/lee#"
response.Write(RegExpTest(pt,str))
输出结果:
0。匹配的长度为32
sdf 222 dsfse
#lee#sdf 222 dsfse#/lee#
--------------
32。匹配的长度为16
我是中国人
#lee#我是中国人#/lee#
--------------
浙公网安备 33010602011771号