Skip to Content
GREP in InDesign
book

GREP in InDesign

by Peter Kahrel
October 2008
Intermediate to advanced
53 pages
1h 57m
English
O'Reilly Media, Inc.
Content preview from GREP in InDesign

Repeat: Sequences of Characters

Without GREP, to replace sequences of space characters with just a single space, you had to repeatedly replace two spaces with one until there were no more double spaces left. GREP makes this much simpler; search any sequence of more than one space and replace with a single one: find \x{20}\x{20}+ and replace with \x{20}.

As we’ve seen earlier, the plus is a repeat character and means “at least once,” so that the expression paraphrases as “find a space followed by a sequence of at least one space.” There are several repeat characters; they’re all in the Repeat flyout (see Figure 7). We’ll deal with them in turn.

? Zero or One Time

To find, say, both British and American spellings harbour of harbor, search for harbou?r. The scope of ? is just one character, so in this example it applies only to the u. The expression paraphrases as “find harbo, perhaps followed by u (or, followed by zero or one occurrence of u), followed by r.” To extend the scope of ? to more characters, group them with parentheses: (19)?\d\d finds all two-digit numbers (matched by \d\d), as well as four-digit numbers whose first two digits are 19. You can combine this with alternation: (18|19|20)?\d\d matches two-digit numbers as well as four-digit numbers that begin with 18, 19, or 20.

The ? operator is useful for matching numbers as well—for example, in dates. The expression \d\d-\d\d-\d\d\d\d finds dates formatted like 08-04-2007. But dates come in different shapes, so to allow ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GREP in InDesign CS3

GREP in InDesign CS3

Peter Kahrel
HTML5 for Publishers

HTML5 for Publishers

Sanders Kleinfeld

Publisher Resources

ISBN: 9780596157173Errata Page