6

So, I'm just beginning to understand Regular Expressions and I've found the learning curve fairly steep. However, stackoverflow has been immensely helpful in the process of my experimenting. There is a particular word macro that I would like to write but I have not figured out a way to do it. I would like to be able to find two words within 10 or so words of each other in a document and then italicize those words, if the words are more than 10 words apart or are in a different order I would like the macro not to italicize those words.

I have been using the following regular expression:

\bPanama\W+(?:\w+\W+){0,10}?Canal\b

However it only lets me manipulate the entire string as a whole including random words in between. Also the .Replace function only lets me replace that string with a different string not change formatting styles.

Does any more experienced person have an idea as to how to make this work? Is it even possible to do?


EDIT: Here is what I have so far. There are two problems I am having. First I don't know how to only select the words "Panama" and "Canal" from within a matched Regular Expression and replace only those words (and not the intermediate words). Second, I just don't know how to replace a Regexp that is matched with a different format, only a different string of text - probably just as a result of a lack of familiarity with word macros.

Sub RegText()
Dim re As regExp
Dim para As Paragraph
Dim rng As Range
Set re = New regExp
re.Pattern = "\bPanama\W+(?:\w+\W+){0,10}?Canal\b"
re.IgnoreCase = True
re.Global = True
For Each para In ActiveDocument.Paragraphs
  Set rng = para.Range
  rng.MoveEnd unit:=wdCharacter, Count:=-1
  Text$ = rng.Text + "Modified"
  rng.Text = re.Replace(rng.Text, Text$)
Next para
End Sub

Ok, thanks to help from Tim Williams below I got the following solution together, it's more than a little clumsy in some respects and it is by no means pure regexp but it does get the job done. If anyone has a better solution or idea about how to go about this I'd be fascinated to hear it though. Again, my brute forcing the changes with the search and replace feature is a little embarrassingly crude but at least it works...

Sub RegText()
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 = "\bPanama\W+(?:\w+\W+){0,13}?Canal\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, m.Length
        rng.Font.ColorIndex = wdOrange
    Next m
  End If

Next para

Selection.Find.ClearFormatting
Selection.Find.Font.ColorIndex = wdOrange
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
    .Text = "Panama"
    .Replacement.Text = "Panama"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.ColorIndex = wdOrange
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
    .Text = "Canal"
    .Replacement.Text = "Canal"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Font.ColorIndex = wdOrange
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.ColorIndex = wdBlack
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
6
  • The Match object has an Index property which tells you where in the text the match occurred. You can use that to address particular ranges to change their formatting. If you update your question to show your existing code someone may chime in with suggested changes. Commented Jul 6, 2012 at 2:53
  • show me the list of words you're running the regex on.
    – jay
    Commented Jul 6, 2012 at 3:17
  • 1
    Is it words are more than 10 words or words are more than 10 letters or sentences are more than 10 words?
    – Cylian
    Commented Jul 6, 2012 at 4:11
  • @TimWilliams I'll look into the match property and post whatever happens when I get the chance, I've never really done more than basic VBA so I'm not sure I'll be able to get much out of it though.
    – pavja2
    Commented Jul 6, 2012 at 4:32
  • @jared I don't have a specific list of words but rather a bunch of news articles and I want to be able to italicize particular pairs of words that might not be next to each other - for example "global" within 10 words of the word "war"
    – pavja2
    Commented Jul 6, 2012 at 4:34

2 Answers 2

6

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
4
  • This does a great job finding the text and changing its format, the real issue I have been having is that all the macros I do can only change entire phrases (instead of just the words "panama" and "Canal"). So for example the above macro highlights all of the words in "The Panama project of a canal" instead of just the 2 and last word of that phrase, it might just be that what I am trying to do is impossible...
    – pavja2
    Commented Jul 7, 2012 at 2:41
  • Not impossible: I just didn't do the whole thing for you :-) You know the first word will be panama, so just hilight that (you know where it starts, and it's length). The last word is canal, so just hilight that as well. Just basic math at this point... Commented Jul 7, 2012 at 2:49
  • Yeah, I actually figured out a way around it - it's not very graceful but I'l l post it in my original question one I get it working all the way. Thanks for the help, this is pretty much exactly what i was looking for.
    – pavja2
    Commented Jul 7, 2012 at 2:55
  • Updated my post with the solution I worked out. It seems to work consistently but it also pretty ugly, however this is the only way I could think of that would make it feasible to change the words in the macro without much of a hassle (like if I was looking for Nietzsche and Epistemology instead of Panama and Canal). Thank you so much for the help!
    – pavja2
    Commented Jul 7, 2012 at 3:32
0

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

1
  • Perhaps I'm just missing something, my issue is that I can only change the entire regex match (i.e. all of the words between "foo" and "bar" are also changed). I don't know how to take matches and then only change the words "foo" and "bar" without effecting any other words within the matched text. A secondary problem that's more a result of my not having good google-fu or familiarity with VBA macros is that I don't know how to change a matched Regexp's format, just how to replace the content. I updated my question with the macro I'm currently working with.
    – pavja2
    Commented Jul 6, 2012 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.