我一直在用 VB6 写的各种需要正则的小程序里引入这个模块, 很简洁很好用:)
使用之前当然需要在 工程 -> 引用 里勾选 Microsoft VBScript Regular Expression 5.5
具体用法嘛…
1, StrReplace(正则替换):
MsgBox StrReplace("1d2e3f","\d","a")
将会输出
adaeaf
2, StrMatch(正则查找):
Dim mhs As MatchCollection
Set mhs = StrMatch("1d2e3f","\d")
If mhs.Count > 0 Then
For i = 1 To mhs.Count
MsgBox mhs(i).Value
Next i
End If
具体效果嘛…能猜到了吧? 猜不到的自己试试去… 其他用法请参见MSDN 🙂
下面附上模块源码:
Attribute VB_Name = "Regex"
Option Explicit
Public Function StrReplace(StrSource As String, StrPattern As String, StrNew As String) As String
Dim NewReg As RegExp
Set NewReg = New RegExp
NewReg.IgnoreCase = True
NewReg.Global = True
NewReg.Pattern = StrPattern
StrReplace = NewReg.Replace(StrSource, StrNew)
End Function
Public Function StrMatch(StrSource As String, StrPattern As String) As MatchCollection
Dim NewReg As RegExp
Set NewReg = New RegExp
NewReg.Global = True
NewReg.IgnoreCase = True
NewReg.Pattern = StrPattern
Set StrMatch = NewReg.Execute(StrSource)
End Function
学过。不知道忘到哪里去了。。
这里只是regex的用法,为何需要Implements?又不是去实现Class的Sub和Function。
不用声明 Implement?
不懂…
您牛怎么可能不知道 Implements……
什么意思啊- -||||
都是我不懂的,坐沙发!
呵呵,这可是给人感觉已经是上世纪的东西啊=.=||