Members Only

GREP of the Month: Negating Characters

Learn how to exclude a single character (or a range of them) with these handy GREP expressions.

This article appears in Issue 88 of InDesign Magazine.

grep-level-easy

Learning how to use the absence or negation of a certain character as a search criterion will give you a powerful way of finding what you want. And you can use two different expressions for this for maximum flexibility.

Negating character classes?The first expression is really simple to use. Capitalize the common character classes like d (any digit), u (uppercase), or s (space) to invert their effect. Thus, D will find everything that’s not a digit, and S will find anything that’s not a space. For example, this is especially useful when searching for words that aren’t capitalized, but should be.

Negating specific characters?You can also negate certain characters in your search by using the [^] pattern. To use it, type all the characters you want to be excluded after the ^ symbol without spaces. Don’t worry, InDesign will treat all characters as individual entries and not as one combined search. For example, you can use the expression [^abc] to find any character except for a, b, and c. If you need to negate a range of characters, you can use the usual character range expression (such as [^0-6]) to exclude all numbers from 0 to 6.

Here’s a more complex example. The expression [^42]d{5} will find a six-digit number. The first character can be anything as long as it isn’t 4 or 2, followed by any five regular digits. Below, you can see the found entries in cyan.

grep-negating

Be sure to experiment with this technique; it’s too powerful not to use!

—Bart Van de Wiele

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 (4)

Leave a Reply

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

Loading comments...