Word find and replace with regular expressions | rotation operator

When using a regular expression to search for text, I am mistaken somewhere.

This is the code I'm 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 Handlingor Event Handling (EH)in a passage, but the OR statement does not work.

When I try to find Event Handlingseparately, his job. similarly for Event Handling (EH). But together with the OR operator, |it does not work. Why?

+4
source share
2 answers

The built-in Find function only supports a limited set of regular expressions. If you want to use the usual standard standard expressions, you need 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

, . , Event Handling \(EH\) "(Event Handling \(EH\)|Event Handling)", Event Handling , , , (EH).

, Word, Find, - Event Handling \(EH\) :

'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
+4

Microsoft Word Find and Replace , . " " "" / "".

MVP , :

+1

All Articles