Regex Word Macro that finds two words within a range of each other and then italicizes those words?

*爱你&永不变心* 提交于 2019-12-04 05:57:57

I'm a long way off being a decent Word programmer, but this might get you started.

EDIT: updated to include a parameterized version.

Sub Tester()

    HighlightIfClose ActiveDocument, "panama", "canal", wdBrightGreen
    HighlightIfClose ActiveDocument, "red", "socks", wdRed

End Sub


Sub HighlightIfClose(doc As Document, word1 As String, _
                     word2 As String, clrIndex As WdColorIndex)
    Dim re As RegExp
    Dim para As Paragraph
    Dim rng As Range
    Dim txt As String
    Dim allmatches As MatchCollection, m As match

    Set re = New RegExp
    re.Pattern = "\b" & word1 & "\W+(?:\w+\W+){0,10}?" _
                 & word2 & "\b"
    re.IgnoreCase = True
    re.Global = True

    For Each para In ActiveDocument.Paragraphs

      txt = para.Range.Text

      'any match?
      If re.Test(txt) Then
        'get all matches
        Set allmatches = re.Execute(txt)
        'look at each match and hilight corresponding range
        For Each m In allmatches
            Debug.Print m.Value, m.FirstIndex, m.Length
            Set rng = para.Range
            rng.Collapse wdCollapseStart
            rng.MoveStart wdCharacter, m.FirstIndex
            rng.MoveEnd wdCharacter, Len(word1)
            rng.HighlightColorIndex = clrIndex
            Set rng = para.Range
            rng.Collapse wdCollapseStart
            rng.MoveStart wdCharacter, m.FirstIndex + (m.Length - Len(word2))
            rng.MoveEnd wdCharacter, Len(word2)
            rng.HighlightColorIndex = clrIndex
        Next m
      End If

    Next para

End Sub

If you're after just doing each 2 words at a time, this worked for me, following your practice lines.

foo([a-zA-Z0-9]+? ){0,10}bar

Explanation: will grab word 1 (foo), then match anything that is a word of alphanumeric characters ([a-zA-Z0-9]+?) followed by a space (), 10 times ({0,10}), then word 2 (bar).

This doesn't include full stops (didn't know if you wanted them), but if you want to just add . after 0-9 in the regex.

So your (pseudocode) syntax will be similar to:

$matches = preg_match_all(); // Your function to get regex matches in an array

foreach (those matches) {
    replace(KEY_WORD, <i>KEY_WORD</i>);
}

Hopefully it helps. Testing below, highlighted what it matched.


Worked:

The foo this that bar blah

The foo economic order war bar

Didn't Work

The foo economic order. war bar

The global foo order has been around for several centuries, over this period of time people have evolved different and intricate trade relationships dealing with situations such as agriculture and bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!