Regular Expressions and Shortcuts, Part 3.5

This post is part of a series on Regular Expressions and their applications in the Shortcuts app.

Regular Expressions and Shortcuts, Part 1

Regular Expressions and Shortcuts, Part 2

Regular Expressions and Shortcuts, Part 3

Before continuing on and looking at how to integrate the regular expression we’ve been talking about so far into a shortcut, I want to incorporate a piece of feedback from Allister Jenks.

I originally came up with this:


Allister points out that there’s a much more logical and concise way of doing it.

He’s right, of course. The following works perfectly and makes more sense.


Actually, we don’t even need the + after the first group, thanks to greedy matching. By default, a regular expression will expand to match a pattern as far into a string as possible. This means that the group (?:.+\/) is already going to match as many times as possible.

As a result, we can get the same result with this regular expression:


In order to make the first group non-greedy, so that it will stop after the first forward slash, we could put a question mark at the end of the group, inside the parenthesis.


If we add this change to the full regular expression, it fundamentally changes our capture group (.+) though.


This will still match each line of our file path listing, but the part that is captured is now everything after the first foward slash up to the period for the file extension, as shown below.

The updated, non-greedy capturing regular expression

Given that I check for beginning of line and end of line in my regular expression, and given that it’s probably best to use the most concise expression possible, maybe I should have just written this from the beginning:


If nothing else, all this should illustrate some key points about regular expressions:

  • I’m no expert or anything close to it,
  • There are usually multiple ways to achieve the same thing,
  • Sometimes an expression will give the desired results but could still be written in a way that’s more expressive of how regular expressions themselves work.

In my case, adding {1,} or even + after the (?:.+\/) group served to mask the fact that the group will perform greedy matching and capture an entire file path like hugo-files/data/links/ without having to use a modifier to tell it “one or more times” at all.

Next time we’ll take a break from regular expression theory and look at how the full regular expression I’ve been talking about these first 3 posts is used inside of Shortcuts.