該模式可爲例如: - (Section \d+[\r\n])(\w+(?: \w+)?)
。
第一個捕獲組捕獲第一行(部分,數字和換行符)。
第二個捕獲組捕獲評級(一個或兩個詞) ,這就是你的實際需要。
下面你有一個Word文檔的示例腳本,檢查(用作宏)。
Sub Re()
Dim pattern As String: pattern = "(Section \d+[\r\n])(\w+(?: \w+)?)"
Dim regEx As New RegExp
Dim src As String
Dim ret As String
Dim colMatches As MatchCollection
Dim objMatch As Match
ActiveDocument.Range.Select
src = ActiveDocument.Range.Text
Selection.StartOf
With regEx
.Global = True
.MultiLine = True
.pattern = pattern
End With
If (regEx.Test(src)) Then
Set colMatches = regEx.Execute(src)
ret = "Matches " & colMatches.Count & ": "
For Each objMatch In colMatches
ret = ret & vbCrLf & objMatch.SubMatches(1)
Next
Else
ret = "Matching Failed"
End If
MsgBox ret, vbOKOnly, "Result"
End Sub
非常感謝!這正是我正在尋找的! –