正则表达式的三种用法
-----------------------------------------------------------------------------
Sub 替换()
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = "\D"
For Each Rng In [a1:a4]
Cells(Rng.Row, 2) = .Replace(Rng, "")
Next
End With
End Sub
-----------------------------------------------------------------------------
Sub 提取()
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = "\S+"
For Each Rng In [b1:b4]
Set mat = .Execute(Rng)
For Each m In mat
y = y + 1
Cells(Rng.Row, y + 2) = m '将匹配的内容依次存放在数据单元格后面
Next
y = 0
Next
End With
End Sub
-----------------------------------------------------------------------------
Sub 验证()
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = "abc|abb\b"
For Each Rng In [b2:b10]
If .Test(Rng) Then
n = n + 1
Cells(n + 1, "d") = Cells(Rng.Row, 1) '将匹配成功的A列的内容放到D列
End If
Next
End With
End Sub
-----------------------------------------------------------------------------