Fan of creative technology, elearning, instructional design and a little geeky

GREP it and sort it

Digitip 070 – Adobe InDesign

During the GREP session at the InDesign Conference in Melbourne late August last year, I demoed the “swapping of first name and surnames” and have been meaning to add a tip on this to my blog for sometime in which we GREP it and then sort the names alphabetically. Last night’s visit to the Brisbane IDUG, incidently my first visit to an Australian IDUG, in which Iain presented a great series of short InDesign tips and tricks reminded me 🙂

The pattern

GREP is all about search for patterns in text and applying format or pattern changes to found text. Let’s make the following assumption. A tab-delimited file was placed in InDesign, possibly originating from Excel. Names are listed as: “FirstName <tab> LastName”So how do we find these with use of GREP and swap them around? Well, let’s review the pattern we are looking for: we are looking for “a series of characters” that is followed by a “tab-character” and ends with another “series of characters”. Time to start looking.


Select Find/Change… from the Edit menu and click the GREP tab. This change the Find/Change dialog to “GREP” mode.


Marking Subexpression

Subexpressions are like smaller ‘sub-parts’ of a Find that can be reused for various purposes. To find the above pattern we’ll use subexpressions. We’ll need at least two subexpressions. One for the “first name” and one for the “surname”. Select Match > Marking Subexpression from the “Find What” Metacharacters menu. Insert two subexpressions.


A marking subexpression is an expression that may be recalled. Non-marking ones can not be recalled.

“()()” is now listed in the “Find What” field.The round brackets encapsulate the subexpressions. Between the two subexpression we are looking for a Tab character. Insert the cursor between the two subexpressions. Next Select Tab from the “Find What” Metacharacter menu.

“()\t()” is now listed in the “Find What” field

Wildcard characters

Within each of the subexpressions you’re now looking for any character that might appear one or more times. As the first name or the surname can contain spaces. Place the cursor between the brackets of the first subexpression and select Wildcards > Any Character from the “Find What” Metacharacters menu. Repeat this for the second subexpression.


“(.)\t(.)” is now listed in the “Find What” field

One or More times

Your’re not looking for single character first or surnames but for one or more characters.Place the behind the ‘.’ inside the subexpression. Select Repeat > One or More Times from the “Find What” Metacharacters menu. Repeat this for the second subexpression.


“(.+)\t(.+)” is now listed in the “Find What” field


We’re now ready to make the change. When we are finding things we are finding a pattern that contains two subexpressions. In the “Change To” field we want to return the second expression first and the first expression second. Insert your cursor in the “Change To” Field. Select Found > Found 2 from the “Change To” Metacharacter menu to return the second subexpression info first, then Select Found > Found 1.


“$2$1” is now listed in the “Change To” field. I’d like for the Names Listing to be formatted using the NamesList paragraph style. This paragraph style uses a nested style called “Bold” that formats the surname in “Bold”. However as surnames could consist of more than one word we’d need to insert another type of delimiter through which we can set the change for Nested Styles. The End Nested Style Character Here will do the job 🙂 To prep the changed text for this, insert your cursor between the $2 and $1. Select Other > End Nested Style Character Here from the “Change To” Metacharacters menu.
Add a Space behind the ~h to prevent the first and surnames from becoming one word.
“$2~h $1” is now listed in the “Change To” field.

Change Formatting

You’re ready to set the Change Formatting now. Click the Format Settings button next to the Change Formatting to set the Change Format settings.

If you don’t see this part of the dialog, click More Options.


Set the Paragraph style to “NamesList”, click OK.

Making the change

You are now ready to make the change. Select the Names Listing text you’d like to apply the change to. Ensure the Search option is set to “Selection”.


Next press Change All.


Sorting the paragraphs

Time to sort the paragraphs now. highlight the names listing again, and Show the Scripts panel (Window > Automation). From the Application/JavaScripts/Sample Script double click “Sort Paragraphs”.


You’re now done. Listing now displays Surname Lastname and is sorted in alphabetical order.

Similar posts
  • Adobe InDesign and XML: A Reference Guide The following is a reference guide I have compiled over time for those of you who are looking at doing a little more with XML in InDesign (originally published Feb. 2011, last updated Feb 2018). If you have any extra information you’d like to see added to this guide, feel free to message me. Mapping XML tags to InDesign [...]
  • How to change the colour of bullet points in InDesig... A few weeks ago I recorded a short quick tip tutorial. InDesign’s Control panel, gives users a quick and easy way to format text as a bulleted list. Highlight the text, and click the Bulleted List button in the Paragraph Formatting Controls mode for the Control panel. This applies a universal bullet character as the [...]
  • Creating a pop-up window in a PDF with Adobe InDesig... In the following YouTube tutorial, we’ll create an interactive PDF from Adobe InDesign, in which we click on a button, which in turn opens up a simple pop-up window containing a close-box. When the close-box is clicked the pop-up window disappears [...]
  • InDesign: Facing pages with odd-numbered left pages Back in 2004 I wrote a tip for InDesign CS on how you could have the first page of your document be a left page AND have it start with page number 1. It’s hard to believe this is almost a decade ago… But a recent question on Facebook, made me think I should really [...]
  • Negative Lookbehind – GREP for Designers This is the fourth and final blog-posts in a series of posts on lookaheads and lookbehinds in GREP, written after speaking at the Perth InDesign User Group. My speaker notes are also available: What is GREP? (PDF download) (2.4Mb). In the previous three posts I briefly introduced GREP, and we took a look at Positive [...]


  1. Cari Jansen Cari Jansen
    July 21, 2008    

    🙂 GREP is a serious time saver!

  2. July 15, 2008    

    Thank you! I’m just learning GREP, and this just helped me over a hurdle. Yay for Marking Subexpressions!

Leave a Reply

Your email address will not be published. Required fields are marked *