VB script 正则表达式的使用
发布网友
发布时间:2022-04-20 00:25
我来回答
共1个回答
热心网友
时间:2022-07-12 07:01
这是我做的一个txt阅读器里的 正则值过滤方式的 两个函数,你拿去参考吧
' =====================正则值过滤方式
Private Function REGEXP0()
Dim rFind As RegExp
Dim S As String, sChap As String, n As Long
Dim rMatch As Match, rMatchCol As MatchCollection
Dim lngItem As Long
Me.Caption = "正在创建目录请稍后..."
sFile = Trim$(Filename)
'If Len(sFile) = 0 Then Exit Sub
'If Len(Dir$(sFile)) = 0 Then Exit Sub
Set rFind = New RegExp
rFind.IgnoreCase = True: rFind.Global = True
rFind.Pattern = TxtFindx
iMemos = 0
ReDim iMemo(0), iChapNum(0), sChapName(0), sText(0)
iMemo(0) = 1: iChapNum(0) = 0: sChapName(0) = "(Index)": sText(0) = ""
Open sFile For Input As #1
Do While Not EOF(1)
' DoEvents
Line Input #1, S: n = n + 1
lins = lins + 1
If rFind.Test(S) Then
iMemos = iMemos + 1
ReDim Preserve iMemo(iMemos), iChapNum(iMemos), sChapName(iMemos), sText(iMemos) '重新定义数组
If (iMemos > 1) And (n - iMemo(iMemos - 1) < 10) Then '找到相同的项删掉
List1.RemoveItem List1.ListCount - 1
End If
sText(iMemos) = S
iMemo(iMemos) = n
iChapNum(iMemos) = 0
sChapName(iMemos) = "(Unknown)"
Set rMatchCol = rFind.Execute(S)
For Each rMatch In rMatchCol
If InStr(TheChapWord, "," & rMatch.SubMatches(1) & ",") Then
sChap = rMatch.SubMatches(0)
iChapNum(iMemos) = UCaseToNum(sChap)
sChapName(iMemos) = Trim$(Replace(rMatch.SubMatches(2), " ", ""))
Exit For
End If
Next
List1.AddItem iMemos & "." & Trim$(sText(iMemos)) '加载目录
End If
Loop
ReDim Preserve iMemo(iMemos + 1), iChapNum(iMemos + 1), sChapName(iMemos + 1)
iMemo(iMemos + 1) = n + 1
iChapNum(iMemos + 1) = 0
sChapName(iMemos + 1) = "(End)"
Close #1
End Function
'==================正则值函数==========
Private Function UCaseToNum(S As String) As Integer
Dim c As String, S1 As String, S2 As String, s0 As String, i As Long
S1 = "一二三四五六七*"
S2 = "123456789"
s0 = "零○0"
c = Trim$(S)
If Left$(c, 1) = "十" Then c = "1" & c
If Right$(c, 1) = "十" Then c = c & "0"
If Right$(c, 1) = "百" Then c = c & "00"
For i = 1 To 9
c = Replace(c, Mid$(S1, i, 1), CStr(i))
c = Replace(c, Mid$(S2, i, 1), CStr(i))
Next
For i = 1 To Len(s0)
c = Replace(c, Mid$(s0, i, 1), "0")
Next
c = Replace(c, "十", "")
c = Replace(c, "百", "")
c = Replace(c, "两", "2")
UCaseToNum = Val(c)
End Function