---MODIFIED--- VBA Word - 在单词中找到“-”并留下单词
---MODIFIED--- VBA Word - Find "- " inside a word and left the word
旧
-----我怎么能那样做?我试过这个,但它崩溃了 ant execute
Sub Sintesis()
With ActiveDocument.Content.Find
.Text = "([^$])" & "-^p"
.Replacement.Text = ""
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End sub
有帮助吗?在有人告诉我之前,我更喜欢使用 find/replace 而不是 Regex
结束旧-----
我发现我正在部分解决我的实际问题。我真正需要的是用 "" 删除 "- "string between the two halfs of a word. I can't replace "-" 因为我的文本有一些像第一个一样正确的字符串。
我真的需要转换这个:
"Blablablab- labla"
进入:
"Blablablablabla"
您可以使用 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
已解决:
Sub FindDash()
With Selection.Find
.Text = "([0-9A-z])- ([0-9A-z])"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
旧 -----我怎么能那样做?我试过这个,但它崩溃了 ant execute
Sub Sintesis()
With ActiveDocument.Content.Find
.Text = "([^$])" & "-^p"
.Replacement.Text = ""
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End sub
有帮助吗?在有人告诉我之前,我更喜欢使用 find/replace 而不是 Regex 结束旧-----
我发现我正在部分解决我的实际问题。我真正需要的是用 "" 删除 "- "string between the two halfs of a word. I can't replace "-" 因为我的文本有一些像第一个一样正确的字符串。
我真的需要转换这个: "Blablablab- labla" 进入: "Blablablablabla"
您可以使用 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
已解决:
Sub FindDash()
With Selection.Find
.Text = "([0-9A-z])- ([0-9A-z])"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub