简体   繁体   中英

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.

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

But how to exclude for example a specefic one like 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; for that you need 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)

Anyway, this is a good article on wildcard searches in 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. 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. 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).


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

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...

As already noted by others, this is not possible in Microsoft Word's flavor of regular expressions. 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 ). 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.

In your particular case, I would use this:

\\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]. In a similar vein, you can do something like (?<!\\bSEDWS12WW04|\\bSEDWS12WW05|\\bSEDWS12WW05) to exlude several IDs.

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.

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.

Best regards, Stanislav

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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