Regex 如何将文本样式更改为vba foreach循环
我有这个正则表达式代码,它工作得很好,但我试图操纵foreach中的文本,但对文档没有效果。 我想更改所选内容的样式并切断标记Regex 如何将文本样式更改为vba foreach循环,regex,vba,ms-word,Regex,Vba,Ms Word,我有这个正则表达式代码,它工作得很好,但我试图操纵foreach中的文本,但对文档没有效果。 我想更改所选内容的样式并切断标记 Sub Substituir() Set documento = ActiveDocument.Range Dim texto As String Set oRegExp = New RegExp oRegExp.Pattern = "<h1>[\s\S]*?</h1>" oRegExp.Global = True oRegExp.Mult
Sub Substituir()
Set documento = ActiveDocument.Range
Dim texto As String
Set oRegExp = New RegExp
oRegExp.Pattern = "<h1>[\s\S]*?</h1>"
oRegExp.Global = True
oRegExp.MultiLine = True
Dim resultado As MatchCollection
Set resultado = oRegExp.Execute(documento)
For Each r In resultado
r.Find.Execute FindText:="<h1>", ReplaceWith:="", Replace:=wdReplaceAll
r.Find.Execute FindText:="</h1>", ReplaceWith:="", Replace:=wdReplaceAll
Next
End Sub
然后在word中将样式更改为标题1。只有在无法通过内置通配符搜索实现所需内容时,我才会使用正则表达式
Option Explicit
Sub Substituir(Optional ByVal this_tag As String = "h1", Optional ByVal this_replacement_style As String = "Heading 1")
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = "(\<" & this_tag & "\>)(*)(\</" & this_tag & "\>)" ' Default is "(<h1>)(*)(</h1>)"
.Replacement.Text = "\2"
.Format = True
.Replacement.Style = this_replacement_style
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
End Sub
选项显式
子取代基(可选ByVal this_标记为String=“h1”,可选ByVal this_替换_样式为String=“Heading 1”)
使用ActiveDocument.Content
和…一起找
.ClearFormatting
.Text=“(\)(*)(\)”默认值为“()(*)()”
.Replacement.Text=“\2”
.Format=True
.Replacement.Style=此替换样式
.Wrap=wdFindContinue
.MatchWildcards=True
.Execute Replace:=wdReplaceAll
以
以
端接头
是否只想将bla-bla-bla
替换为bla-bla-bla
呢?请尝试oRegExp.Pattern=“([\s\s]*?)”
然后将匹配的代码替换为documento.Text=oRegExp。替换(documento.Text,$1”)
是,我需要将bla-bla-bla-bla-bla-bla-bla-bla-bla-bla-bla替换为bla-bla-bla-bla-bla-bla-bla-bla-bla-,例如,斜体。最好是将其样式更改为标题1。您可能只需将文本复制到.html文件中,然后在Word中打开它
bla bla
Option Explicit
Sub Substituir(Optional ByVal this_tag As String = "h1", Optional ByVal this_replacement_style As String = "Heading 1")
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = "(\<" & this_tag & "\>)(*)(\</" & this_tag & "\>)" ' Default is "(<h1>)(*)(</h1>)"
.Replacement.Text = "\2"
.Format = True
.Replacement.Style = this_replacement_style
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
End Sub