Members Only

GREP of the Month: Repeat Zero or One Time

A perfect expression when sometimes you see it, and sometimes you don't!

This article appears in Issue 71 of InDesign Magazine.

My family happened to be living in England when, as a child, I was first learning to read and write. So I blame my parents (of course) for the fact that I vacillate between typing color and colour, or story and storey. This is relevant in this month’s GREP lesson because in both cases, a letter may or may not appear. Fortunately, GREP makes it easy to search for characters that may or may not be there: simply follow the character with a question mark (?).

For example, in GREP, colou?r will find either color or colour. Similarly, if you are trying to apply formatting to HTML tags in your text, you might use a GREP style to search for </? em> which would find <em > and also </em>— because the question mark means the slash can be there or not. Here’s another example: Let’s say you have a product code that is a series of numbers, sometimes followed by a capital letter. You could type \d+ [A-Z]? to find codes both with and without the final letter. Okay, let’s say you wanted to find both September and the abbreviation Sept.

Remember that when you place a sequence of characters in parentheses, GREP acts like you’ve grouped them. So you can search for Sept(ember)? and it will find both the full and the partial word.

You can even expand that trick to search for Sept(ember)? \d+(rd|th|nd)? and it will find September 9th, Sept 19, Sept 22nd, and other similar variations.

Similarly, you can find the words longterm, long-term, and long term with a single expression: long[\w-]?term One of the core concepts of “fuzzy logic” is that data may or may not be present and you need to plan for either case. The question mark helps you do that in GREP.

Bookmark
Please login to bookmark Close

Not a member yet?

Get unlimited access to articles and member-only resources with a CreativePro membership.

Become a Member

Comments (1)

Leave a Reply

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

Loading comments...