GREP commands in InDesign

November 21, 2023

There will be times you’re laying out a file for a client, that will need several repetitive changes throughout the project. The file might be several charts of figures with perfectly spaced-out columns in the Word, but when you import the file, the formatting is all off. You then find out instead of using tabs they used spaces between the columns. When this happens, you can go through and highlight each grouping and replace with the tabs or you can use a GREP command to do all of this for you in a few secs.

When I started to learn GREP I only found 2 ways that GREP was explained online; either they just tell you the command to put in or they would list all the codes but not how they worked together. I’ll try to explain in this post of the how to start working in GREP. There is so much more that can be done with this tool, you can search online to go deeper with info, this will, hopefully, help you to understand the basic building of the commands.

To access GREP, it’s in the find menu:

GREP in InDesign is in the find area go to Edit > Find/Change (or hit Command + F; Control +F)

In this window there’s a bar of commands, select ‘GREP’. This does include some prebuild searches you can use; we’ll go over a couple and how to modify them when your project needs a bit more.

Change multiple tabs to one tab:

A simple one to start with is if the client sent in a file with multiply columns and instead of putting in a tab stop for each, they used the default ones and just hit tab to get to the area they needed.

Commands to know:
\t = tab (this is for a single tab character)
+ = The plus sign is added behind the element saying there will be more than one of these.

In GREP:

Below is the Find (F) and the Change (C) commands. In the Find we’re looking for more than 1 tab (\t = tab; + = more than one). Then in the Change we’re making it a single tab. In the spots the client only had one tab that won’t change but if they used 4 tabs that will now be 1 tab. After running you’ll need to add your tab stops if you didn’t have those set before.

F: \t+
C: \t

You can also use the preset of multiply spaces to singe space then in the Change switch to ‘\t’ we’ll go over what all is going on in the Find area later.

Converting multiple periods to single tab:

When several ‘….’ periods are used to separate columns instead of a tab with leader.

[.] = anything in the [] is searching for anything in the brackets; in this case we’re looking for a period.

{2,} = the {} this will check for number of times that is inside the there, here it’s 2. The comma after the 2 is saying it can be that number or higher like: 3, 4, 5, etc. will trigger all of them in the find to be replaced. If they are using eclipses in this file then you’ll want to use 4, so those eclipses won’t get triggered.

F: [.]{2,}
C: \t
(Replacing what was found to a tab, if you need to have the periods, you’ll add them when you set the tab in the tab leader.)

Change multiple spaces into a tab:

From drop down select ‘Multiple Space to Single Space’. With this preset, the file is using spaces for separating columns this will replace the spaces and you can change them to a tab. However, if they used 2 spaces at the end of the sentence, then those would change to a tab also. To fix this change the {2,} to a {3,} like below, the 2 is saying 2 together, going to the 3 will ignore the issue with 2 spaces after a period as it will look for 3 or more; the comma after the 3 means or more.

F: [~m~>~f~|~S~s<~/~.~3~4~%]{3,}
C: \t

The above looks complicated and scary when first looking at GREP commands and a good example of what you’re seeing isn’t as complex as it looks. What the above is doing is looking for everything in between the [] with 3 or more of those together.

Inside the [] which is the part that could scary people away from learning, this is just looking for every type of blank space. If you were to create this yourself in InDesign using the @ dropdown in whitespace section you would select each of those to go inside the [] and get the same thing, here’s what each stand for:

~m = Em Space
~> = En Space
~f = Flush Space
~| = hair space
~S = Nonbreaking Space (fixed width)
~s = Nonbreaking Space
< = thin space
~/ = Figure Space
~. = Punctuation Space
~3 = Third Space
~4 =  Quarter Space
~% = Sixth Space

Most of those are not commonly used but when they write these, they go with the more complex to make sure to cover everything. You should be able to get by with:

[\s]{3,} = this is just looking for a space (\s) that is 3 or more times {3,}

Apply character style:

The document contains titles to 2 books, and you need to apply a style to those titles

In the find you’ll add the book titles with the | (shift + \) between the title names, this is case sensitive.

F: Green Eggs and Ham|Cat in the Hat

In Change Format (bottom of the window): select the character style you’ve created for book titles, like ‘Book Title’ this will apply to all that are found in the search.

For the above if ‘Ham’ might also be ‘ham’ you can add in the search to check both upper and lower so it would look like:

F: Green Eggs and (H|h)am|Cat in the Hat

Placing the part that needs to be placed inside the () this is to have a subcategory check then the | to be an OR. With the above Ham will be trigger if it’s an ‘H’ OR ‘h’. Doing the sub-category it saves from having to type the whole title out again just for a lower-case letter.

To add to this a bit more if you needed to add the registered trademark ® to the titles you can add this to the change.

C: $0~r
The $0 = take what is found in the found search
~r = is the symbol for Registered Trademark, you can get the symbols and others options by clicking the @ symbol next to find/change and using the dropdown to go to symbols and select it.

When you do the replace all now it will have the ® and have the style applied to it.

Notes:

Search dropdown (under change):

All Documents = all the documents you have open.
Document = The document you’re currently working in.
Story = The text box that is currently active.
Selection = Area you’ve highlighted only.

Save your search:

To save your used queries click the icon by the top drop down that looks like an arrow pointing down to a computer. Then name it to easily remember. I named the above ‘Multi space (3+) to 1 Tab’

Search using or (|) for words that are sub of another word:

When doing a search and one word is part of another word (like ‘the’ would show in ‘there’) put the biggest word first in your search (‘there’) or it will find the smaller word in the bigger word and only change that part.

Search for word only and not as a sub of the word:

If a word could be a sub of another word, you’ll want to add the ‘word boundary (/b)’ to it:
Ex. \bthe\b will exclude ‘there’ ‘them’ ‘other’, etc. if you used it just at the beginning ‘\bthe’ you’ll get ‘there’. Something like this will find almost all the ‘the’ in the file: \b(?i)the\b(?:[ [:punct:]])

GREP Commands

A lot of using GREP is setting up groupings of commands to find what you’re looking for. Here’s some, not nearly of all the commands out there to start your searches.

^ = beginning of Paragraph;
^The = will find ‘The’ that is at the beginning of a paragraph
\r or $= has to be at the end of a paragraph. Include punctuation.  Example: The.$ = where ‘the’ would be at the end of a paragraph.  ‘said[ [:punct:]]\r’ this will find the word ‘said’ with any punctuation at the end of a paragraph.

? = Zero or One Time
+ = One or more times
() = grouping of information to find
{} = times to match, needs a number inside and a , if more than; include 2 numbers for a at least and not more than. Examples: the{2,3} = find the that shows up together 2 or 3 times only.
A{2} = matches exactly 2 times, if AA are together like AARP
A{2,} = matches at least 2 or more times
A{2,}? = matches Only 3 times (shortest match)
A{2,3} = matches at least 2 times and not more than 3
A{2,3}? = matches at least 2 times and not more than 3 (shortest match)
(?i)the = this will search for the word ignoring case. It will find ‘the’, ‘The’ and ‘THE’
(?-i)the = This will search for the word after the (?-i) as the case this will only find ‘the’ not ‘The’ or ‘THE’, this is the standard search and will only need in certain cases.
[] = search for anything that is in the brackets, [abc] it will look for any a, b or c. If looking for any numbers 1-5 you can write it [1-5] which will look for 1, 2, 3, 4, or 5.
. (period) = Any character in search (wildcard), in replace adds a period
\s = White space, space or tab for search, in replace it adds a space
\S = not a white space
\w = Any word character
\W = Any not word character
\u = any uppercase character
\U = Any not uppercase character
\l = Any lowercase character (\ + lower case L)
\L = Any not lowercase character
\b = word boundary wrap the word between two of these to find just the word and not a sub word of it. Use at front or back if only that part needs to be in the word. If you put at front then ‘the’ would be found in ‘there’ put at the end ‘there’ won’t be found.

[ [:punct:]] = any punctuation symbol
| = (shift + \) this is the OR command as in I’m looking for this OR that.
$0 =($ zero) use this in the change section to put in what was found from the found field.

Related Articles

Acrobat order form

Acrobat order form

Working with a non-profit organization for their yearly fundraiser, where they would go and sell fruit, meat, and cheese gift boxes. Due to the COVID we tried something different and created a fillable PDF that people can fill out their orders and email to the...

read more
2016-2017 Community Guide

2016-2017 Community Guide

Fifth year for the Community Guide, inside layout was updated from previous year, new photos added to guide. New style cover to stand out from previous years. Redesigned a few of the ads from the previous year. Download Copy

read more
Kids Book – Pumpkin & Peanut go to the Circus

Kids Book – Pumpkin & Peanut go to the Circus

The book ‘Pumpkin & Peanut go to the Circus’ started off as a painting series in the style of a kids storybook; the paintings were done with acrylic on watercolor paper. My cousin-in-law, Amy C. Lamb, seen the painting and created a story to go along with them....

read more

Pin It on Pinterest

Share This