Find More Than One Paragraph with GREP
GREP is, by default, set up to find text inside a paragraph. That is, if you search for .+ (one or more of any character), then it will find those characters from the beginning of the paragraph to the end of the paragraph, and then stop. That’s it. But what if you’re trying to match a pattern across multiple paragraphs?
For example, what if you know you have three paragraphs in the middle of your story, and the first paragraph starts with “Giraffes” and the last paragraph ends with “fish.” You can do this with the code ^Giraffes(\r|.)+fish.$

The trick there is a simple one: the code inside the parentheses says find “any character or a return character.” (The caret means “beginning of paragraph,” the dollar sign means “end of paragraph,” and the vertical bar means “or”.)
By specifically including the return character, we can find more than one paragraph. That’s why the code ^(.+\r)\1+ means “Find duplicate paragraphs/lines in a list.” In other words, “find everything in the paragraph–including the paragraph marker–followed by one or more exact duplicates of that.” If you want to remove the duplicate(s), then type $1 into the Replace With field.

Just remember that this code won’t work with GREP Styles, because those are specifically for applying formatting within a single paragraph!
This article was last modified on December 30, 2021
This article was first published on June 10, 2014
Commenting is easier and faster when you're logged in!
Recommended for you
A Script to List Find/Change Query Results in InDesign
A lot of applications that have a feature for finding and replacing text give yo...
Remove the Numbers at the Beginning of a List with a Simple GREP
You've copied a bunch of numbered paragraphs from a web page and now you want to...
Swap Names Around with GREP and Find/Change
Joe needs to convert all his captions in the form LastName_FirstName.jpg into "F...
