有没有办法重置/清除所有比赛的RegEx


Dim regExp As RegExp
Dim regMaC As MatchCollection
Dim regMat As Match
For Each A in B
regExp.Pattern = "(w*)"  ' Doesn't match in 2nd run, keeps (W*) match
Set regMaC = regExp.Execute(A.Body)
If regMaC Is Nothing Then
Debug.Print "No match in 1"
Else
For Each regMat in regMaC
Debug.Print regMat.Submatches(0)
Next regMat
End If
regExp.Pattern = "(W*)"  ' Matches in 1st run
Set regMaC = regExp.Execute(A.Body)
If regMaC Is Nothing Then
Debug.Print "No match in 2"
Else
For Each regMat in regMaC
Debug.Print regMat.Submatches(0)
Next regMat
End If
' Would like to clear all matches here
Next A

在这个例子中,匹配被保留到第二次运行中,但是否有一个简单的方法可以从第二个模式中清除匹配。如果第一个表达式保持匹配并覆盖上一个匹配,这就不是问题。

添加以下语句作为循环内的第一个语句:

regExp = new RegExp

备注:

您可能还想在循环的中途执行此操作(取决于您的期望(,并可能将Global属性设置为True

最新更新