Replacing regexes in Drafts

If you’re a longtime regular expression user, you may be surprised at how the Find and Replace feature in Drafts works. The Find part is normal, but the Replace is a little different.

Let’s assume we have a set of book titles with subtitles set off by colons1:

The Films of Fritz Lang: Allegories of Vision and Modernity
In Pharaoh's Army: Memories of the Lost War
Understanding Comics: The Invisible Art
A Matter of Interpretation: Federal Courts and the Law
The Digital Dialectic: New Essays on New Media
I Saw Esau: The Schoolchild's Pocket Book

We’d like to reformat these titles by putting each subtitle on an indented line under the main title and then separating them all with blank lines. Like this:

The Films of Fritz Lang
  Allegories of Vision and Modernity

In Pharaoh's Army
  Memories of the Lost War

Understanding Comics
  The Invisible Art

A Matter of Interpretation
  Federal Courts and the Law

The Digital Dialectic
  New Essays on New Media

I Saw Esau
  The Schoolchild's Pocket Book

For compatibility with the formatting here on the blog, I’ve indented the subtitles above with two spaces, but let’s say we really want them indented with a tab.

If we were working in Perl, and had these titles in a text file, we could do the reformatting with this one-liner:

perl -pe 's/^(.+): (.+)$/$1\n\t$2\n/' titles.txt

where the find regex is

^(.+): (.+)$

and the replace regex is


Most regular expression engines nowadays take their cues from Perl (Perl 5, anyway), so although Python isn’t really set up for one-liners, the find and replace regexes we’d use in a Python script are the same.

BBEdit’s “Grep” find and replace is basically the same,

BBEdit find and replace

The only difference is the use of backslashes instead of dollar signs to identify the parts captured by parentheses.

So if we wanted to do this reformatting in Drafts, it’s only natural to try this:

Perlish replace

As you can see from the lower half of the screenshot, the finding part worked as expected, but if we tap the Replace All button, we get this:

The Films of Fritz LangntAllegories of Vision and Modernityn
In Pharaoh's ArmyntMemories of the Lost Warn
Understanding ComicsntThe Invisible Artn
A Matter of InterpretationntFederal Courts and the Lawn
The Digital DialecticntNew Essays on New Median
I Saw EsauntThe Schoolchild's Pocket Bookn

What’s happened is the \n and \t in the replacement regex have not been interpreted as newline and tab. Instead, the backslashes have been interpreted as “treat the next character as literal, even if it would normally have a special meaning in a regex,” which is why we see an nt between the main titles and subtitles and an n at the end of each line.

So how do we get what we want? Don’t overthink it. If you want a newline in the replacement, type Return; if you want a tab, type Tab. Like this:

Replace with newline and tab

It’s hard to see because the Replace field isn’t tall enough to show three lines at once, but there’s a Return after the $2.

This is actually easier, but because of years of using \n and \t, I almost always type it the Perlish way first.

By the way, if you’re wondering whether you should use “real” tabs and newlines or the backslashed metacharacters in the Find field, the answer is Yes. You can use either one.

  1. I plucked these from this GoodReads page