vba 之匹配
正则对象有四个属性和三个方法。
属性 说明 方法 说明
| Global Boolean值,指明单次匹配或者全部匹配 | Execute 将正则模式应用于字符串,并返回Matchs集合 |
|IgnoreCase Boolean值,指明匹配模式是否大小写敏感|Replace 替换正则模式匹配的文本|## 标题
|Pattern 指定用于搜索的正则模式 | Test 对字符串执行正则匹配,返回Boolean值说明匹配是否成功|
Multiline 指定的搜索字符串分布在多行
Sub RegExpDemoReplace()
Dim Res()
Dim objRegEx As Object
Dim objMH As Object
Dim j As Integer
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Pattern = "[^!]([A-Z]{1,3}\d{1,7})"
objRegEx.Global = True
For i = 1 To Cells(Rows.Count, "F").End(xlUp).Row
form = " " & Cells(i, "F")
pre = Cells(i, 1) & "!"
Set objMH = objRegEx.Execute(form)
If objMH.Count > 0 Then
newform = form
For j = 0 To objMH.Count - 1
ref = objMH(j).submatches(0)
newform = Replace(newform, ref, pre & ref)
Next
Cells(i, "I") = Trim(newform)
End If
Next
Set objRegEx = Nothing
Set objMH = Nothing
End Sub