ASP正则表达式方面小笔记

比较随意的一些ASP正则表达式方面的笔记(或学习小甜品) =====================================

<%
' 方法说明:Set RsObj = Server.CreateObject快捷
Function [&rg](ByRef rgRef, ByVal pe, ByVal ig, ByVal gb, ByVal [?Casually])
Call RgNew(rgRef, pe, ig, gb,[])
End Function

' 清空正则表达式对象
Function [!rg](ByRef rgRef)
Set rgRef = Nothing
End Function

' 测试是否符合正则表达式
Function InRg(ByRef rgRef, ByVal str)
InRg = rgRef.Test(CStr(str))
End Function

' 获取正则表达式匹配的个数
Function RgNum(ByRef rgRef, ByVal str)
RgNum = Clng("0"&rgRef.Execute(str).Count)
End Function

' 创建或重新初始化一个正则表达式
Function RgNew(ByRef rgRef, ByVal pe, ByVal ig, ByVal gb, ByVal [?Casually])
If Not IsObject(rgRef) Then
Set rgRef = New RegExp
ElseIf rgRef is Nothing Then
Set rgRef = New RegExp
End If
If Not IsEll(pe) Then
rgRef.Pattern = pe
End If

If Not IsEll(ig) Then
rgRef.IgnoreCase = Bool(ig)
End If

If Not IsEll(gb) Then
rgRef.Global = Bool(gb)
End If
End Function

' 判断是否"有"还是"没有"
'
判断是否"是"还是"不是"
'
判断是否"存在"还是"不存在"
'
判断是否"有用"还是"没用"
'
.........等等
Function Bool(ByVal val)
If isEll(val) Then
Bool = False
ElseIf TypeName(val) = "Interger" And Int(val) = 0 Then
Bool = False
ElseIf TypeName(val) = "String" Then
Select Case LCase(Cstr(val))
Case "","0","false"
Bool = False
End Select
ElseIf TypeName(val) = "Boolean" Then
Bool = CBool(val)
Else
Bool = True
End If
End Function

' 省略或缺少了
Function IsEll(ByVal val)
IsEll = CBool(isNull(val) Or isEmpty(val) Or TypeName(val)="Error")
End Function
:
Function IsLack(ByVal val)
IsLack = IsEll(val)
End Function

' 正则表达式调试
'
举例:
dim r : [&rg] r,"\bdreamyoung(\d{0,4})_([^dreamyoung]+)\b", , ,[]
debugReg r,"dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$",,[]
Function debugReg(ByRef rgRef, ByVal str, ByVal endFlag, ByVal [?Casually])
[<<ln] "<div style=""font-weight:bold; margin:10px; padding:10px; border-color:#036; border-style:dashed; border-thick;"">"
[<<ln] "regExp.<b><font color=red>Pattern</font></b> = " & rgRef.Pattern
[<<ln] "regExp.<b><font color=green>IgnoreCase</font></b> = " & rgRef.IgnoreCase & "&nbsp;&nbsp;&nbsp;<font color=#CCCCCC>(False For Default)</font>"
[<<ln] "regExp.<b><font color=blue>Global</font></b> = " & rgRef.Global & "&nbsp;&nbsp;&nbsp;<font color=#CCCCCC>(False For Default)</font>"
[<<ln] "<font color=#000000>Matches Is A Dictionary For Match Object</font>" & _
"&nbsp;&nbsp;<font color=#CCCCCC>Matches Come From : Set Matches = RegExp.Execute(str)</font>"
[<<ln] "<font color=#000000>Matche Come From : Set Matche = Matches.Item(i)</font>"
[<<ln] "<font color=#000000>SubMatches Is A Dictionary For String</font>" &_
"&nbsp;&nbsp;<font color=#CCCCCC>SubMatches Come From : Set SubMatches = Match.SubMatches</font>"
[<<ln] "<font color=#000000>SubMatche(String) Come From : SubMatch = SubMatches.Item(i)&nbsp;&nbsp;&nbsp;<font color=#CCCCCC>(Tip:No ""Set"" , Because It is only a string!)</font></font>"
'[<<!] TypeName(str)
If Not IsEll(str) Then
[<<] "---------------------------------------------------"
[<<ln] "---------------------------------------------------"
[<<ln] "Input String: " & Server.HTMLEncode(str)
[<<ln] "regExp.Test("""&str&""") = " & rgRef.Test(str)
' Matches集合(包含0或多个Match对象)
Dim mes : Set mes = rgRef.Execute(str)
Dim rgCount : rgCount = mes.Count
[<<ln] "regExp.Execute("""&str&""").Counts = "& rgCount
If rgCount > 0 Then
' Match对象(含各种属性)
Dim m0 : Set m0 = mes(0)
[<<ln] "regExp.Execute("""&str&""").Item(0) = " & m0
[<<ln] "regExp.Execute("""&str&""").Item(0).FirstIndex = " & m0.FirstIndex
[<<ln] "regExp.Execute("""&str&""").Item(0).Length = " & m0.Length
[<<ln] "regExp.Execute("""&str&""").Item(0).Value = " & m0.Value

' SubMatches集合(包含0或多个字符串)
Dim submes : Set submes = m0.SubMatches
Dim smCount : smCount = submes.Count
[<<ln] "regExp.Execute("""&str&""")(0).SubMatches.Counts = "& smCount
If smCount > 0 Then
Dim sm0 : sm0 = submes(0)
[<<ln] "regExp.Execute("""&str&""")(0).SubMatches.Item(0) = " & sm0 & Space(10) & "<font color=#CCCCCC>This is the 1st sub match, For More:</font>"
Dim i
For Each subStr In submes
[<<ln] "regExp.Execute("""&str&""")(0).SubMatches.Item("&(i-0)&") = " & subStr
i = i + 1
Next
End If
End If
End If
[<<ln] "</div>"

If Bool(endFlag) Then
[<<!] []
End If
End Function
%>

测试的输出结果为:


 


regExp.Pattern = \bdreamyoung(\d{0,4})_([^dreamyoung]+)\b
regExp.IgnoreCase = False   (False For Default)
regExp.Global = False   (False For Default)
Matches Is A Dictionary For Match Object  Matches Come From : Set Matches = RegExp.Execute(str)
Matche Come From : Set Matche = Matches.Item(i)
SubMatches Is A Dictionary For String  SubMatches Come From : Set SubMatches = Match.SubMatches
SubMatche(String) Come From : SubMatch = SubMatches.Item(i)   (Tip:No "Set" , Because It is only a string!)
------------------------------------------------------------------------------------------------------
Input String: dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$
regExp.Test("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$") = True
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$").Counts = 1
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$").Item(0) = dreamyoung2011_sw.hf,
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$").Item(0).FirstIndex = 0
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$").Item(0).Length = 21
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$").Item(0).Value = dreamyoung2011_sw.hf,
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$")(0).SubMatches.Counts = 2
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$")(0).SubMatches.Item(0) = 2011 This is the 1st sub match, For More:
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$")(0).SubMatches.Item(0) = 2011
regExp.Execute("dreamyoung2011_sw.hf,dreamyoung2012_sw.hf and dreamyoung2013_sw.hf$")(0).SubMatches.Item(1) = sw.hf,


原文地址:https://www.cnblogs.com/dreamyoung/p/2436334.html