is now part of CreativePro.com!

Finding Duplicate List Entries with GREP

This article appeared in Issue 69 of InDesign Magazine.

When you use the Find/Change dialog box, you can specify certain criteria in the Find field and then reference your found text using the “Found Text” expression in the Change To field, visualized as a $ symbol followed by a number. For example, if you search for ^\s(.) and replace with $1, you will find any character preceded by a space located at the beginning of a paragraph; and you’ll change it to just the “any character” you found—the $1 refers to the GREP expression inside the parentheses. Understanding this principle will help you when you use the next expression.

Using found text again

Curiously (and this is not even documented in InDesign), you can use the same “Found Text” expression in the Find field to find double entries in a list. But using the $ symbol is not going to work, because the $ symbol in the Find field means end of paragraph. For this reason, you should use the \1 expression. 

For example, let’s say you type (.+)\r\1 in the Find field, and set the Change To field to $1. The expression (.+) finds a series of characters in a paragraph, and \r will find the hard return at the end of the paragraph, allowing the GREP search to continue looking based Find Duplicate List Entries How to find the same name, product, or numbers repeated twice in a row. on whatever expression comes next. Finally, \1 reuses the same expression as the GREP in the first pair of parentheses (hence the number 1). So in this case you’re looking for text, followed by a return, followed by the exact same text: a double entry! If the Change To field were blank, the pair would be deleted. But when you set it to $1, InDesign replaces the pair with just the first instance—it “de-dupes” the list.

You can take this one step further and work with different sets of parentheses, and then use multiple found criteria like \2, \3, \4, etc.

Bart Van de Wiele is a LinkedIn Learning author and public speaker currently working as a Principal Solutions Consultant at Adobe. Visit his website at bartvdw.com.
>