• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

GREP to keep two words together

Explorer ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

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.

TOPICS
Performance , Print , Scripting

Views

714

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jul 03, 2023 Jul 03, 2023

[Just for comment]

 

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

 

(^/)  The Jedi

Votes

Translate

Translate
Community Expert ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

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.

Screenshot 2023-07-03 at 8.22.16 AM.png

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

[Just for comment]

 

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

 

(^/)  The Jedi

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines