【问题标题】:Use wildcards to find all excluded matching strings使用通配符查找所有排除的匹配字符串
【发布时间】:2019-08-21 18:50:44
【问题描述】:

我正在尝试改进已成功使用的 VBA 宏。该宏的目的是搜索 Word 文档,查找并突出显示与给定查找条件不匹配的特定文本字符串。

当搜索结果是整数时,此宏非常有效。但是当涉及前导零时,它不会找到并突出显示。

Sub Find_Highlight_Height()
    Application.ScreenUpdating = False
    Options.DefaultHighlightColorIndex = wdYellow
    'Options.DefaultHighlightColorIndex = wdRed
    With ActiveDocument.Content.Find
      .ClearFormatting
    '  .Text = "Height {1,}-[!0]{1,}*mm"
      .Text = "Height {1,}-[!0]*(mm)>"
      With .Replacement
        .Text = "^&"
        .ClearFormatting
        .Highlight = True
      End With
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
End Sub

我的意思是: 我不想匹配的文本字符串。 “高度-0mm”(是的,“t”和“-”之间有一个或多个空格)

所以我想匹配的所有其他字符串(举几个例子)“Height -1mm”、“Height -1.0mm”、“Height -10.5mm”和“Height -0.5mm”。

上述通配符.Text = "Height {1,}-[!0]*(mm)>" 对除前导零(最后一个)之外的所有结果都按预期工作。我已经尝试了这么多通配符字符串的组合,我已经记不清了

我错过了什么?有没有更好的方法来进行这种“逆向查找”和突出显示?

【问题讨论】:

  • "Height {1,}-[0-9.]*(mm)>"怎么样
  • ...或者没有* 甚至
  • Tim,它只能是一个排除的零(希望除 0mm 之外的所有结果)被突出显示。看起来正在发生的事情是它看到了排除 [!0] 并且这忽略了模式的其余部分 *(mm)>
  • 您提供的示例对我有用。它也会检测到(例如)Height -0.0.1mm。没有“零或一个实例”标志有点问题。编辑 - 我只是看到你明确不想匹配 Height -0mm
  • 不确定它是什么,但排除 [!0] 后的句点/点似乎使 find 函数忽略了模式的其余部分,因此它不会标记结果。跨度>

标签: vba ms-word full-text-search wildcard


【解决方案1】:

感谢 Freeflow,我能够将其包括在内以捕获小数点。请参阅下面的工作代码

我最终不得不循环查找两次。第一次捕获不是“高度 -0mm”的所有内容(95% 的结果)。然后第二次抓到剩下的结果中包含小数点。

' Use to search and highlight all matching from the phrase ".Text =" line below
'
' Explane the ".Text = "Height {1,}-[!0]{1,}" wildcard pattern match
' Height - (just the begining text string to search for)
' [!0] (telling to find anything except the number zero)
' {1,} ( telling the find that there may be one or more in a row)

Sub Find_Highlight_Height()

Dim loopTimes As Integer
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdYellow
'Options.DefaultHighlightColorIndex = wdRed

' Looping thru twice so can capture find's with and without decimal points
' Discovered an issue with VBA in that the decimal point halts the find pattern when present
' right after a exclude "!" pattern element this case the "[!0]"
For loopTimes = 1 To 2

    With ActiveDocument.Content.Find
    .ClearFormatting

    If loopTimes = 1 Then
       .Text = "Height {1,}-[!0]{1,}*mm" 'Works when decimal point is NOT present
    Else
        .Text = "(Height)( {1,})(-)([0-9]{1,})(.)([0-9m]{1,})" 'Works when decimal is present
    End If

    With .Replacement
        .Text = "^&"
        .ClearFormatting
        .Highlight = True
    End With
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With

Next loopTimes

Application.ScreenUpdating = True

End Sub

我仍然对为什么 VBA 在模式中排除小数后停止通配符模式感到困惑。

【讨论】:

    猜你喜欢
    • 2023-01-06
    • 2015-07-29
    • 2012-09-26
    • 2017-11-14
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多