Skip to main content
Inspiring
July 3, 2023
Answered

GREP to keep two words together

  • July 3, 2023
  • 2 replies
  • 1822 views

Is there an easy way to search for 2 or more words/characters and keep together on same line so they don't break?

 

e.g. Dr name, Mrs name, 10 September, pp. 1-10

 

I have a long list of words that I don't want to break and have to do manual searches.

This topic has been closed for replies.
Correct answer FRIdNGE

[Just for comment]

 

\b(Dr|Mr|Ms|Mrs|Prof)\.?\K\h

 

(^/)  The Jedi

2 replies

Community Expert
July 3, 2023

There are a lot of permutations of salutations, what if one is write as Dr and another as Dr. 

 

I usually do a find and replace for the space after the word for example

 

Find (after the last \K is a space)

\b(Dr.?|Mr.?|Ms.?|Mrs.?|Prof.?)\K

 

Replace with 
~S

 

You could also replace this No Break character style

Replace with - leave blank

And in the change to style change it to No Break character style

 

You can also use this in a GREP style for the paragraph - again using a No Break style (as @Manan Joshi suggested - just tacking on to the idea. 

 

For the months


Date format what you describe 10 September

\b\d{1,2}\K (?=(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b)

 

I choose the shortened version in case you have dates like 10 Sep

That GREP will only find the space between the words. 

 

My personal preference is to replace the space with a non-breaking space. 

That way if you we were to say add a Bold style to one of the names or the dates - you might inadvertently remove the no break style. 

 

A non-breaking space as a character is a bit better - but manual find and replace. 

 

The Style is a great option but it can inadvertently add the style to words you didn't mean to add it to.

 

Each way has it's caveats. 

I prefer the non-breaking space method.

 

FRIdNGE
FRIdNGECorrect answer
July 3, 2023

[Just for comment]

 

\b(Dr|Mr|Ms|Mrs|Prof)\.?\K\h

 

(^/)  The Jedi

Community Expert
July 3, 2023

So strange - it didn't work for me earlier - hence my approach - but it's working now.

Need more coffee... thank you for this, I'm a bit confused why it works sometimes and not others... but seems to be ok now.

Community Expert
July 3, 2023

Create a character style with the no break property set and then in the Find/Change Dialog in the Grep section search for something like

Find What : Dr\s\w+

Change Format: Apply your created character style.

You can do so for each entry you have.

-Manan

-Manan