Learning Regular Expressions to Streamline Daily Writing Exercises

One of the key things about learning a craft and getting better at it is practice. Practice is how you learn to use the tools you have and how you find your own voice with them. It’s why the Leica Year or one camera/one lens are interesting exercises for photographers. But what about writing? Most of the advice for writing that I received was “write, write, write!” Which is well and good, but what do I write about?

Fortunately, other people have had also thought about this problem and worked out solutions. I learnt about CM Mayo’s daily writing exercises through Felicia Day’s blog when she was writing about how she wrote The Guild. Her blog was one that I followed on Google Reader (RIP), and way back in 2009, she wrote How I Started Writing1. That immediately set me off on the road to working on daily writing exercises.

Here’s the problem: I’ve been working on those exercises off and on for several years but, I don’t generally stick with the routine for more than a few weeks.

The first problem is friction. The process usually involved me going to the website, finding the exercise for the day, copying it down occasionally, and then starting a timer for 5 minutes and writing until the timer went off. I’ve found that this friction – look up the exercise for the day, copy it to a document, and then write it – is enough to keep me from working on the exercises for more than a few weeks at a time. Which is a shame because the exercises are great, and I enjoy working on them.

I’ve written in Day One, on loose leaf sheets of paper, in Drafts, and in more apps and formats that I can shake a stick at. None of them fix the issue of having too many hurdles before I start writing.

The second problem that I find is that if I open a browser, I am very likely to get distracted by something else. Reddit, Hacker News, YouTube, or one of the hundreds of tabs that I usually have open.

To avoid that issue, I copied every single exercise into a file that I saved in Drafts. That itself didn’t change the friction with getting started. I still had to open the draft, find the exercise for the day, copy it to iA Writer, start a timer and get writing. Still, I had removed one major hurdle from my flow. I wasn’t getting distracted by all the open tabs in my browser.

Then Apple bought Workflow and released it as Shortcuts. It’s an app that I use from time to time, but not as frequently as my brain comes up with ideas to automate with Shortcuts. Listening to Rosemary Orchard and David Sparks talk about how they’ve automated everything on Automators doesn’t help with curtailing those ideas. But I did think about further streamlining the writing process with the help of a shortcut. That would make everything easier and I would write more frequently!

Yeah…

I’ve spent a few months trying to do this, and until just recently, it hasn’t worked very well. In fact, it hadn’t worked at all. First, I couldn’t get Drafts and Shortcuts to play nicely together. Then I stopped using Drafts, so I copied the whole text directly into a shortcut. The initial plan was to split the document into daily files, but the thought of doing this for 365 days worth of prompts drove me to despair.

I had seen a few posts on Reddit about using Regular Expressions with Shortcuts and then fell down that rabbit hole. First, I inserted dividers between each day’s prompts. Manually pasting 366 ------ was probably not the best use of my time, but when you automate something, you have to adhere to the XKCD automation rules.

Anyway.

I now had a start point for each day (“January 1” or “April 15”, for example), and an ending point. Try as I might, I couldn’t figure out how to get the text between those two. Either it would select the entire text from the start point (January 26 until EOF, for example), or it would match nothing.

As an aside, I tried using ChatGPT to help me generate the expression. Unlike the reports I had seen of people using it to write code, I didn’t get anything useful from it. I don’t doubt that it can be a useful tool assuming that you can figure out the right prompt. But for me, I got non-working expressions and the steps to create the shortcut, which I already had because I had thought through my problem and flowcharted it before starting on this insane bikeshedding adventure. Honestly, I thought that this would have been a pretty easy problem to solve. And for someone else, it might have been. But here I was, in a hole so deep that I could barely see the sky above. All I had was the shovel in my hand and the ground beneath my feet that needed to be dug.

I figured out that (January 1) would match the start of the day’s prompt. And then, that (\n-{6}) would capture the end of the section. But I couldn’t figure out how to capture the bit in between those two. You know, the actual exercise. DuckDuckGo sent me to a few Stack Overflow discussions2, but I wasn’t having any luck. Either I would get no matches, or a single match (the entire document from the date I had selected). Through all this, RegExr was incredibly helpful with testing my expressions. I pasted a short section – about 3-4 days of exercises – and ran my tests on those.

How do these two sections work?

(Month Date) works by directly matching the date to today’s date. It matches the exact order of letters and numbers that I pass into the pattern. CM Mayo has each exercise dated and therefore, so does my exercise file. This was the easiest part of this whole process.

(\n-{6}) matches the ------ at the end of each exercise. Looking through the documentation at RegExr, I figured out that I didn’t have to type ------. {6} looks for six of the symbols listed immediately before it. In this case, it’s the hyphen. This pattern matches the ------ at the end of exercise.

Finally, I turned to the second largest search engine in the world, YouTube. After passing through several very basic tutorials about matching phone numbers and email addresses, I came across Wiktor Stribizew’s video 3 that was to the point and I finally had my pattern! The missing part in the middle was [\s\S]*?.

[\s\S]*? breaks down as follows:

  • \s matches any whitespace characters
  • \S matches any non-whitespace characters

We’ve covered all characters using these two patterns. But on their own, they don’t match anything. So the pattern needs to repeat, but only enough to cover a single day’s exercise.

  • * repeats [\s\S] between 0 and unlimited times. That isn’t ideal because it would match the remainder of the document.
  • ? is used to restrict * to as few matches as possible. This is called lazy matching.

So my final expression is (January 1\b)[\s\S]*?(\n-{6}).

Once I had figured out how each of these patterns worked, I could think about how to shorten it. It turns out that I didn’t need the \n at the start of (\n-{6}). But I do want to be able to start writing as soon as the shortcut finishes running. So I don’t want to have to press ↩↩ as soon as I start writing. It might be weird, but I know that this is enough to trip me up and stop me writing. So, I edited the pattern to be (-{6}\n\n). This way, I get the file set up just like I want it and I can start writing. For 5 minutes.

After all that, it was time to build a working Shortcut4.

StepActionComment
1Shortcut comment with sources for all the data.A comment to thank everyone who made this shortcut possible.
2Text for daily 5 minute writing exercisesPut all the text into a Text action.
3Getting the current dateGet the current date and format it so it matches the text above (for example, October 1). Set the variable Today to the date.
4Get the exercise for today and encode the result as a URLGet the exercise for the day by matching (Today\b)[\s\S]*?(\n-{6}). Encode the result as a URL.
5Use the current date to set the filename for the day's exercise.Create a new file with today’s date.
6Use x-callback-url to open iA Writer with today's exercise.Open iA Writer with today’s exercise.

So, how much time does this all really save me? I have no idea, but let me refer you to another XKCD comic about automation. What I can say is that it really was fun getting this shortcut to work.


  1. It’s no longer easily accessible on her own website after a redesign, but I found it with help from The Internet Archive 

  2. Like this or this, for example.  

  3. Matching multiline strings between two strings, or how to match across lines | Regex Quickies with Wiktor Stribiżew 

  4. Hat-tip to Dr. Drang for using this format to explain how a shortcut works

Leave a Reply

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