vba 使用正则表达式查找和替换 Word | 交替运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26903163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word find and replace using regex | alternation operator
提问by user123
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.
我试图在段落中查找Event Handling
或Event Handling (EH)
,但 OR 运算符不起作用。
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?
当我尝试Event Handling
单独查找时,它的工作原理。对于Event Handling (EH)
. 但是与 OR 运算符一起使用时|
它不起作用。为什么?
回答by Jean-Fran?ois Corbett
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:
Word 的内置查找功能仅支持有限的一组正则表达式。如果你想使用完整的、通常的、标准的正则表达式,你必须这样做:
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.
如果您选择您的段落并运行它,文本将按您的意图替换。但请注意,Event Handling \(EH\)
应该在搜索模式交替中排在第一位"(Event Handling \(EH\)|Event Handling)"
,因为如果Event Handling
像您最初编写的那样(EH)
排在第一位,它将首先被替换,而不会留下任何内容。
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:
或者,如果您想使用 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
回答by OldGit
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.
Microsoft Word 的“查找和替换”确实适用于各种正则表达式,但仅适用于有限的一组功能。您需要单击“高级”/“更多”下拉窗格中的“使用通配符”选项。
This MVP article documents how to use this: Finding and replacing characters using wildcards
这篇 MVP 文章记录了如何使用它:使用通配符查找和替换字符