Using GREP to style a Question & Answer article

I’ve recently had to create some repetitive styles for for a Q&A article. As opposed to manually applying the character styles to the interviewer & interviewee’s names. I decided to explore Indesign’s GREP styles, which I’ve discovered is one of Indesign’s best features.

In my case I had the interviewers full name, a shortened version, and the interviewee (ex. Community News, CN, and Steve). For those unfamiliar with GREP, it’s Indesign’s implementation of regular expressions

In computing, a regular expression provides a concise and flexible means to “match” (specify and recognize) strings of text, such as particular characters, words, or patterns of characters.

-Wikipedia

So with GREP styles, we give Indesign a string to search for, and it applies a character style to any matches. The biggest challenge I had was figuring out the exact combination of GREP parameters that would properly select the copy I wanted to style.

This is what I finally came up with.

^(Community.+?\r|CN:.+?\r)

So here’s how to read that mess.

“^” – From the beginning of a line.
“()” – Search for elements within these brackets.
“Community” & “CN” – Look for these two words.
“.+?” – Look for 1 or more of any character.
“\r” – Find a carriage return.
“|” – Or do this.

So in English this line would read:

From the beginning of a line, look for the word “Community” followed by 1 or more characters until you reach a carriage return OR look for “CN:” followed by 1 or more characters until you reach a carriage return.

After that all my style was applied perfectly to both the interviewer’s names, and I didn’t have to manually apply a character style 2 dozen times. Definitely worth the time it took to research and debug the GREP style.