简体   繁体   English

MS Word 2016 中的 REGEX:从搜索中排除一个简单的字符串

[英]REGEX in MS Word 2016: Exclude a simple String from Search

So I read a lot about Negation in Regex but can't solve my problem in MS Word 2016.所以我在 Regex 中阅读了很多关于否定的内容,但无法解决我在 MS Word 2016 中的问题。

How do I exclude a String, Word, Number(s) from being found?如何排除被发现的字符串、单词、数字?

Example:例子:

<[AZ]{2}[A-Z0-9]{9;11}> to search a String like XY123BBT22223 <[AZ]{2}[A-Z0-9]{9;11}>搜索像XY123BBT22223这样的字符串

But how to exclude for example a specefic one like SEDWS12WW04 ?但是如何排除例如像SEDWS12WW04这样的特定对象

Well it depends on what you need to achieve or is this a matter of curiosity... RegEx is not the same as the built-in Advanced Find with Wildcards;好吧,这取决于您需要实现的目标还是好奇心的问题... RegEx 与内置的带通配符的高级查找不同; for that you need VBA.为此,您需要 VBA。

Depending on your need, without using VBA, you could make use of space and return characters - something like this will work for the strings provided: [ ^13][AZ]{2}[0-9]{1,}[AZ]{1,}[0-9]{1,}[ ^13] (assuming you use normal carriage returns and spaces in your document)根据您的需要,在不使用 VBA 的情况下,您可以使用空格和返回字符 - 像这样的东西将适用于提供的字符串: [ ^13][AZ]{2}[0-9]{1,}[AZ]{1,}[0-9]{1,}[ ^13] (假设您在文档中使用正常的回车和空格)

Anyway, this is a good article on wildcard searches in MS Word: https://wordmvp.com/FAQs/General/UsingWildcards.htm无论如何,这是一篇关于 MS Word 中通配符搜索的好文章: https : //wordmvp.com/FAQs/General/UsingWildcards.htm


EDIT:编辑:

In light of your further comments you will probably want to look at section 8 of the linked article which explains grouping.根据您的进一步评论,您可能需要查看解释分组的链接文章的第 8 节。 For my proposed search you can use this to your advantage by creating 3 groups in your 'find' and only modifying the middle group, if indeed you do intend to modify.对于我建议的搜索,您可以通过在“查找”中创建 3 个组并仅修改中间组(如果您确实打算修改)来利用它。 Using groups the search would look something like:使用组搜索将类似于:

([ ^13])([AZ]{2}[0-9]{1,}[AZ]{1,}[0-9]{1,})([ ^13])

and the replace might look like this:替换可能如下所示:

\\1 SOMETHING \\3

Note also: compared to a RegEx solution my suggestion is kinda lame, mainly because compared to RegEx, MS-Words find and replace (good as it is, and really it is) is kinda lame... it's hacky but it might work for you (although you might need to do a few searches).另请注意:与 RegEx 解决方案相比,我的建议有点蹩脚,主要是因为与 RegEx 相比,MS-Words 查找和替换(虽然很好,而且确实如此)有点蹩脚......您(尽管您可能需要进行一些搜索)。


BUT... if it really is REGEX that you want, well you can get access to this via VBA: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word但是...如果它真的是您想要的 REGEX,那么您可以通过 VBA 访问它: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word

And... then you will be able to use proper RegEx for find and replace, well almost - I'm under the impression that the VBA RegEx still has some quirks...然后......然后你将能够使用适当的 RegEx 进行查找和替换,几乎 - 我的印象是 VBA RegEx 仍然有一些怪癖......

As already noted by others, this is not possible in Microsoft Word's flavor of regular expressions.正如其他人已经指出的那样,这在 Microsoft Word 的正则表达式风格中是不可能的。 Instead, you should use standard regular expressions.相反,您应该使用标准的正则表达式。 It is actually possible to use standard regular expressions in MS Word if you use a special tool that integrates into Microsoft Word called Multiple Find & Replace (see http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace ).如果您使用集成到 Microsoft Word 中的称为 Multiple Find & Replace 的特殊工具,实际上可以在 MS Word 中使用标准正则表达式(请参阅http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace )。 This tool opens as a pane to the right of the document window and works just like the Advanced Find & Replace dialog.此工具作为文档窗口右侧的窗格打开,其工作方式与“高级查找和替换”对话框类似。 However, in addition to Word's existing search functionality, it can use the standard regular expressions syntax to search and replace any text within a Word document.但是,除了 Word 现有的搜索功能外,它还可以使用标准的正则表达式语法来搜索和替换 Word 文档中的任何文本。

In your particular case, I would use this:在您的特定情况下,我会使用这个:

\\b[AZ]{2}[A-Z0-9]{9,11}\\b(?<!\\bSEDWS12WW04) \\b[AZ]{2}[A-Z0-9]{9,11}\\b(?<!\\bSEDWS12WW04)

To explain, this searches for a word boundary + ID + word boundary, and then it looks back to make sure that the preceding string does not match [word boundary + excluded ID].解释一下,这会搜索词边界 + ID + 词边界,然后回过头来确保前面的字符串不匹配 [词边界 + 排除的 ID]。 In a similar vein, you can do something like (?<!\\bSEDWS12WW04|\\bSEDWS12WW05|\\bSEDWS12WW05) to exlude several IDs.同样,您可以执行类似 (?<!\\bSEDWS12WW04|\\bSEDWS12WW05|\\bSEDWS12WW05) 的操作来排除多个 ID。

Multiple Find & Replace is quite powerful: you can add any number of expressions (either using regular expressions or using Word's standard search syntax) to a list and then search the document for all of them, replace everything, display all matches in a list and replace only specific matches, and a few more things.多重查找和替换功能非常强大:您可以将任意数量的表达式(使用正则表达式或使用 Word 的标准搜索语法)添加到列表中,然后在文档中搜索所有这些表达式、替换所有内容、在列表中显示所有匹配项以及仅替换特定的匹配项,以及其他一些内容。

I created this tool for translators and editors, but it is great for any advanced search/replace operations in Word, and I am sure you will find it very useful.我为翻译人员和编辑人员创建了这个工具,但它非常适合 Word 中的任何高级搜索/替换操作,我相信您会发现它非常有用。

Best regards, Stanislav最好的问候,斯坦尼斯拉夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM