OLD -----我怎么能这样做?我试过这个,但它崩溃了蚂蚁执行
Sub Sintesis()
With ActiveDocument.Content.Find
.Text = "([^$])" & "-^p"
.Replacement.Text = "\1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End sub
有些帮助吗?在有人告诉我之前,我更喜欢用find / replace而不是Regex来做这件事 END OLD -----
我看到我正在解决我的真正问题。我真正需要的是删除一个单词的两个半部分之间的“ - ”字符串。我不能用“”替换“ - ”,因为我的文本有一些正确的字符串,如第一个。
我实际上需要转换这个: “Blablablab- labla” 成: “Blablablablabla”
答案 0 :(得分:1)
<强>解决强>:
Sub FindDash()
With Selection.Find
.Text = "([0-9A-z])- ([0-9A-z])"
.Replacement.Text = "\1\2"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
答案 1 :(得分:0)
您可以使用RegEx执行此操作。使用以下递归过程
Sub ReplaceHyphens() ' Make sure you add a reference to Microsoft VBScript Regular Expressions 5.5
Dim rex As New RegExp
rex.Pattern = "(.)-(.)"
rex.Global = False
Dim str As String: str = ActiveDocument.Content
Dim mtch As Object
If rex.Test(str) Then
ActiveDocument.Content = rex.Replace(ActiveDocument.Content, rex.Execute(str)(0).SubMatches(0) & ActiveDocument.Content, rex.Execute(str)(0).SubMatches(1))
ReplaceHyphens
Else
Exit Sub
End If
End Sub