我在文本中多次出现 [word]- [word],例如 self- interest 而不是 self-interest,后者是正确的形式。我尝试使用 MS Word 的通配符删除笔划后的多余空格。在“查找和替换”框中,我输入:
查找:*-*
替换为:\1-\2
但是,它不起作用。实现此目的的正确形式是什么?
答案1
VBA:
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