之前没有理解好,还以为是学习笔记有谬误。
'VBA正则笔记 肯定环视 Public Sub RegExHandle() Dim Regex As Object Dim Mh As Object, OneMh As Object Dim Text As String Text = "Halo world!" Set Regex = CreateObject("VBScript.RegExp") With Regex .Global = True '.MultiLine = False .Pattern = "(?=Ha)w+" 'Halo .Pattern = "w+(?=lo)" 'Ha End With If Regex.Test(Text) Then Set Mh = Regex.Execute(Text) For Each OneMh In Mh Debug.Print OneMh.Value Next End If Set Regex = Nothing End Sub
.Pattern = "(?=Ha)w+" 得到的字符串是 Halo,模式(?=Ha)是指从H前面的位置开始匹配,且不消耗Ha两个字符,因此得到的就是Halo。
.Pattern = "w+(?=lo)" 得到的字符串是 Ha ,模式(?=lo)是指匹配截止到字符l前面的位置为止,且不消耗lo两个字符。
而非捕获性分组 模式.Pattern = "(?:Ha)w+" 或.Pattern = "w+(?:lo)"都可以匹配字符串Halo