Find URLs and Email Addresses in InDesign with GREP

How to use GREP Find/Change and GREP styles to automate the process of formatting URLs and email addresses, and prevention hyphenation in them.

In this article we are dealing with patterns. Specifically with text patterns that are found in internet and email addresses. More on this later. First, let’s talk about the basics of GREP.

Introducing GREP

InDesign has offered GREP Find/Change and GREP styles for a long time. If you’re wondering what GREP actually is, you’ll be delighted at the possibilities InDesign offers you for editing documents.

GREP itself stands for Global Regular Expression Print. It was originally a UNIX tool used to search for specific code patterns. Today, many text editors offer this tool in their search/replace function, including InDesign.

GREP Find/Change in Adobe InDesign

Controlling Styling and Hyphenation

This is where things start to get interesting. You can use placeholders (any characters, any digits) and a combination of additional GREP commands to search for various types of text content in a document. In this specific case we’ll look at Internet URLs and email addresses.

If you are working on a document in which there are many such addresses, you may want to style them in a special way: in italics, in color, underlined, etc. It is even more important that these addresses are not hyphenated. Because hyphenating a URL in the wrong place will inevitably lead you to an error page (404 – page not found). The easiest way to achieve this is to assign a character style with the language setting No Language to this address.

New Character Style for No Language in Adobe InDesign

Tobias Wantzen has written a very interesting and detailed blog post about this.

A GREP Expression to Find All Internet Addresses

The question now becomes, how do you find arbitrary internet addresses? This brings us back to the term pattern. Such addresses contain elements that occur again and again (or not). For example: www, https://, dots, slashes or even domain endings (.com, .net, etc.)

Over the years, I have repeatedly tinkered with a GREP expression that can track down arbitrary internet addresses in InDesign. Thankfully, I have always received very helpful tips from Gregor Fellenz. You should definitely read his article on automated GREP search/replace with a script he developed.

Not so long ago I was also in discussion with Tobias Wantzen about this and we found a few very useful additions for a GREP expression that can now apparently reliably detect (almost) all occurrences of internet and email addresses. Here it is:

(http|ftp)?(s?://)?[@a-zA-Z0-9.-]{1,256}\.[a-z]{1,6}\S*[^ \)\}\]!?.,;:\r(")(')]

If you paste the code shown above into the Search For field (in the GREP section of the Find/Replace dialog) and start the search, InDesign should find any internet and email address for you. It may be that URLs also use brackets at the end, in which case you would have to adjust the expression accordingly.

To avoid having to enter this long expression every time, you can save it in the Find/Change dialog box.

Saving a GREP Find/Change query in Adobe InDesign

Automate Formatting with a GREP Style

It’s even more useful to put this expression to work in a GREP style as part of a paragraph style to have InDesign apply the above-mentioned styling and/or hyphenation rule automatically.

Decoding the GREP Expression

Admittedly, the expression looks quite complicated, but is basically just a series of individual simple queries. With the “WhatTheGREP” script from Jongware (Theunis de Jong), you can visualize the structure of such expressions directly in InDesign:

Sadly, Mr. de Jong passed away in 2020. He was one of the best script developers for InDesign. Fortunately, many of his scripts are still available on Kasyan Servetsky’s website.

If you want to delve deeper into the subject of GREP search, I recommend the ebook by Peter Kahrel, another script veteran for InDesign: GREP in InDesign, which you can obtain here at CreativePro.com.

Try It Out!

Now have fun finding internet and email addresses! If you stumble upon a case that is not recognized by this expression, please let me know in the comments. I can then try to adjust the GREP query accordingly. A one hundred percent hit rate is difficult to achieve when searching for internet addresses, but we’re already pretty close.

Bookmark
Please login to bookmark Close

This article was last modified on April 17, 2024

Comments (0)

Leave a Reply

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