Reply To: GREP to find first two words of a sentence

Home Page / Forums / General InDesign Topics (CLOSED) / GREP to find first two words of a sentence / Reply To: GREP to find first two words of a sentence

#83969
Peter Kahrel
Member

You want to keep together the first two words in a sentence. So you should exclude the period from the capture:

(?<=\. )\w+? \w

The so-called lookbehind (?<=\. ) reads “if preceded by a period and a space”. This means that the period and the space are included in the search, so they’re matched but not captured. Therefore the ‘no break’ will be applied only to the two words after the period+space.

Note that the way you matched the first two words in a sentence — \w+ \w+ — doesn’t allow either of them to be hyphenated. The formulation above — \w+? \w — allows the second word to be hyphenated because only its first character is applied no-break. The first word is still applied no-break, so it won’t hyphenate. If you want to allow both words at the beginning of the sentence to kept together and to be hyphenated, use this GREP:

Find what: \. \w+\K
Change to: ~S

and blank the Find format and Change format panes. The above query paraphrases as “replace the space between the first two words in a sentence with a non-fixed-width space”. The \K designates a so-called variable-width lookbehind.

Peter

This article was last modified on April 12, 2016

Comments (0)

Loading comments...