
我在文本中有多個 [word]- [word] 實例,例如 self-interest 而不是 selfinterest,這是正確的形式。我正在嘗試使用 MS Word 的通配符刪除筆畫後的無關空格。在尋找和替換框中我輸入:
尋找:*- *
替換為:\1-\2
然而,這不起作用。完成此任務的正確形式是什麼?
答案1
程式設計語言:
WrapReplace
在Word文檔中運作。
Sub WrapReplace()
Call RegExpReplace("(\w+)\-\s(\w+)", "$1-$2")
End Sub
Private Sub RegExpReplace(pattern As String, Backreference As String)
Dim strReplacement As String
Set oRegExp = CreateObject("VBScript.RegExp")
With oRegExp
.Global = True
.IgnoreCase = False
.pattern = pattern
End With
Set matches = oRegExp.Execute(ActiveDocument.Content)
For Each match In matches
Set matchRange = ActiveDocument.Content
strReplacement = oRegExp.Replace(match.Value, Backreference)
With matchRange.Find
.Text = match.Value
.Replacement.Text = strReplacement
.Wrap = wdFindAsk
.Execute Replace:=wdReplaceOne
End With
Next
End Sub