Word find and replace using regex | alternation operator

11,849

Solution 1

Word's built-in Find functionality only supports a limited set of regular expressions. If you want to use the full, usual, standard regular expressions, you have to do something like this:

Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")

With regExp
    .Pattern = "(Event Handling \(EH\)|Event Handling)"
    .Global = True
    Selection.Text = .Replace(Selection.Text, "Replaced")
End With

If you select your passage and run this, text will be replaced as you intended. But note that Event Handling \(EH\) should come first in the search pattern alternation "(Event Handling \(EH\)|Event Handling)", because if Event Handling comes first as you originally wrote it, it will get replaced first, leaving any (EH) behind.

Or, if you want to use Word's built-in Find, then just do two searches — and here too, Event Handling \(EH\) should be the first one:

'Settings
With Selection.Find
    .Replacement.text = "Replaced"
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

'First find
With Selection.Find
    .text = "Event Handling \(EH\)"
    .Execute Replace:=wdReplaceAll
End With

'Second find
With Selection.Find
    .text = "Event Handling"
    .Execute Replace:=wdReplaceAll
End With

Solution 2

Microsoft Word's Find & Replace does work with Regular Expressions of sorts, but only with a limited set of features. You need to click the Use Wildcards option in the "Advanced"/"More" dropdown pane.

This MVP article documents how to use this: Finding and replacing characters using wildcards

Share:
11,849
Admin
Author by

Admin

Updated on June 08, 2022

Comments

  • Admin
    Admin almost 2 years

    While using regex for finding text, I am going wrong somewhere.

    This is the code I am using.

    findText = "(Event Handling|Event Handling \(EH\))"
    Debug.Print findText
    With Selection.Find
        .Text = findText
        .Replacement.Text = "Replaced"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .matchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute Replace:=wdReplaceAll
    

    I am trying to find Event Handling or Event Handling (EH) in the passage, but the OR operator is not working.

    When I try to find Event Handling separately, its working. similarly for Event Handling (EH). But on together with the OR operator | it's not working. Why?

  • Admin
    Admin over 9 years
    sorry.. allMatches is the object right..how can i replace the matched content?
  • Admin
    Admin over 9 years
    i have already used this two search method. I enable the track changes. so for the second time it replaces the track change content also. like this monoclonal antibodies(striked) MAb(MAb(striked))MAb
  • Jean-François Corbett
    Jean-François Corbett over 9 years
    Ok, did you look at my edited Regexp code at the top of my answer.
  • Admin
    Admin over 9 years
    its not working.. Is there any way in VBA to find except track change content??
  • Jean-François Corbett
    Jean-François Corbett over 9 years
    Too long to address in a comment... I suggest you ask a new question. A screenshot illustrating your problem would help.
  • Fjodr
    Fjodr almost 9 years
    Can you add the most relevant informations from this link into your answer?
  • Nor.Z
    Nor.Z almost 2 years
    Is there any way to preserve the Format of the Selected Text after the replacement, when using the regExp?