Skip to main content
Participating Frequently
August 8, 2019
Answered

Using GREP to mark everything that isn't a specific word

  • August 8, 2019
  • 3 replies
  • 4559 views

I'd like to use a GREP style to mark everything, except certain words. So for example:

This is ignore an example ignore text

should be marked like so:

This is ignore an example ignore text

Normally, I'd just have two rules and mark the words I don't want included, but the style I want to apply hides the selected words by setting their size to 0.1 - which can't be reset by another style (the new style would force its size upon it, instead of resetting it).

This is as far as I have come, and this already took me a day to figure out:

.*(?<=[^ignore]).*

It works if the word to ignore is not surrounded by anything. So if the text is just "ignore" it will stay, everything else will be marked. Any help would be appreciated!

This topic has been closed for replies.
Correct answer Jongware

You want to at least mark everything that is "not a word":

\W+

(where the '+' is in the hope that this is more efficient than a single \W, which would act upon each not-a-word character one at a time).

You also want to mark every entire word ...

\W+|\b\w+

– and now everything should be marked; all not-a-word characters OR all word characters. But I'm not done yet.

"After" the "\b" (word break) you are always at the start of a word. At that point you can insert a negative lookahead to exclude the words you are looking for:

\W+|\b(?!(ignore|me)\b)\w+

leading to the result:

You can list all of your to not be ignored inside the inner parentheses, separated with an OR bar |.

3 replies

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
August 10, 2019

You want to at least mark everything that is "not a word":

\W+

(where the '+' is in the hope that this is more efficient than a single \W, which would act upon each not-a-word character one at a time).

You also want to mark every entire word ...

\W+|\b\w+

– and now everything should be marked; all not-a-word characters OR all word characters. But I'm not done yet.

"After" the "\b" (word break) you are always at the start of a word. At that point you can insert a negative lookahead to exclude the words you are looking for:

\W+|\b(?!(ignore|me)\b)\w+

leading to the result:

You can list all of your to not be ignored inside the inner parentheses, separated with an OR bar |.

winterm
Legend
August 10, 2019

A touch of the Master.

Sometimes you may find useful to add to this excellent regex above a Case-insensitive On modifier (?i), say, to pick up a word at the beginning of a sentence.

Inspiring
August 9, 2019

Can’t you just omit the word you’re trying to (ignore) from the first style? Seems backwards to mark an entire paragraph with a character style. Just update the paragraph style. I guess I don’t understand what your end goal is.

Participating Frequently
August 10, 2019

I have different texts / paragraphs with different font sizes, styles, etc. I want to hide everything in them, except for specific words (one specific one, for starters). I hide stuff with a style that makes the text transparent and the font size 0.1. But there doesn't seem to be a way to revert that with a character style, without overriding the font size it should be.

So I'd have to create a paragraph style and a character style for every place I want to use this that has a different look. I'd like to avoid that to keep things manageable. Only thing I can think of is a GREP that hides everything but the specific word(s).

Community Expert
August 9, 2019

You need to do this in two steps: first mark everything, then unmark the words to ignore. The character style to mark everything is applied to ^.+ and marks whole paragraphs. Then the character style to undo the marking is applied to \b(ignore|this|and|that)\b

P.

Participating Frequently
August 9, 2019

Thanks for your input, Peter!

Unfortunately, as I tried to explain in my post, I can't do two steps. I can't create a style to undo the marking, since the marking style changes too many properties that are text specific.

Community Expert
August 9, 2019

Thanks again! Also for clarifying that my solution basically worked by pure chance (none of the words I tested begin with a character from the ignored word).

I have no experience with scripts. I'm using the GREP style for a merge. Can I set it up in a way that it will work automatically in merge previews and the final merge? Or do I have to trigger the script manually every time?


You'd have to run the script every time.

P.