FindBetween: A Useful GREP String

Hey, with the help of Peter Kahrel's GREP in InDesign CS3 book, I was able to figure out how to do something in InDesign that I've always said was possible...

Hey, with the help of Peter Kahrel’s GREP in InDesign CS3 book, I was able to figure out how to do something in InDesign that I’ve always said was possible to do with GREP, but didn’t really know how. Not only is it a handy Find/Change action, but it’s very easy to modify for different situations that designers are often confronted with.

The action is this: Find [some text] that’s in between [whatever], and then apply formatting to just the text, not what’s surrounding it. One example would be formatting parenthetical text without formatting the parentheses themselves: turn (this) into (this) and (that other thing) into (that other thing) all at once, throughout the story or document, with a simple click.

Here’s the GREP expression that finds one or more words of parenthetical content, but doesn’t include the parentheses themselves in the found instances:

(?<=\().*?(?=\))

If you copy and paste that GREP string into the Find What field in the Edit > Find/Change > GREP panel of InDesign CS3, and then click the Find First button, InDesign selects the first instance of parenthetical text, but not the parentheses themselves. Cool!

That means that back in the Find/Change > GREP panel, you can change just the found text’s formatting by specifying what you want in the Change Format area (leave the Change To text field blank). Here I’m specifying that InDesign should italicize parenthetical text, but leave the parentheses untouched:

1-findbetween.gif

Easily Modify the Search
Below is the same GREP string, but this time the characters that govern what the “surrounding items” should be are highlighted in red so you can see what I’m talking about:

(?<=\().*?(?=\))

The first instance is an opening parenthesis, which needs to be “escaped” with a backslash so InDesign knows it’s a literal parenthesis, not some more GREP code. The second instance is the closed parenthesis, again preceded by a backslash to escape it.

I typed these in myself, but it’s simpler to let InDesign drop in the special code that GREP needs. Just choose the special character you want from the dropdown menu next to the GREP Find What field:

1-findbetween2.gif

So, to find text surrounded by a pair of em dashes (but not the em dashes themselves) you’d change both instances of the red characters so it finds an em dash instead of an opening or closing parenthesis. According to the dropdown menu, GREPese for an em dash is a tilde followed by an underscore:

(?<=~_).*?(?=~_)

Here’s the string to find text in between any kind of double quotes, nice and simple:

(?<=).*?(?=)

In case you’re wondering, Peter says this type of GREP search is called a “Lookaround” — a combination of a Lookahead and a Lookbehind. All the “look*” type of GREP searches share one thing in common … they let you tell InDesign to find some text based on a character that precedes or follows it, but not to include that character in the found text itself.

Save the GREP String

If you think you’d find this useful, don’t forget to click the disc icon in the GREP panel of Find/Change so you can save it and recall it from the dropdown menu of saved searches from now on. I called mine “FindBetween.”

Just for fun (and to test out sharing saved searches), I uploaded my FindBetween search … it’s a tiny XML file …. in case you want to download it. Drop it into the GREP folder inside your Preferences > Adobe InDesign > Version 5.0 > Find-Change Queries folder on your hard drive.

Bookmark
Please login to bookmark Close

This article was last modified on December 18, 2021

Comments are temporarily disabled for maintenance.

Comments (131)

Loading comments...

Comments are closed.