第39集 正则表达式3- Pattern常用符号2
169、 []
使用方括号 [ ] 包含一系列字符,能够匹配其中任意一个字符。用 [^ ] 不包含一系列字符,
则能够匹配其中字符之外的任意一个字符。同样的道理,虽然可以匹配其中任意一个,但是只能是一个,不是多个。
'1 和括号内的其中一个匹配
Sub t29()
Dim regx As New RegExp
Dim sr
sr = “ABDC”
With regx
.Global = True
.Pattern = “[BC]” '匹配B和C,结果:AD
Debug.Print .Replace(sr, “”)
End With
End Sub
'2 非括号内的字符
Sub T35()
Dim regx As New RegExp
Dim sr
sr = “ABCDBDC”
With regx
.Global = True
.Pattern = “[^BC]” '匹配不是B和不是C,结果:BCBC
Debug.Print .Replace(sr, “”)
End With
End Sub
'3 在一个区间
Sub t38()
Dim regx As New RegExp
Dim sr
sr = “ABCDGWDFUFE”
With regx
.Global = True
.Pattern = “[A-G]” '匹配A~G之间的每个字母,结果:WU
Debug.Print .Replace(sr,