Using an "IF" statement with a regex match in Apple Shortcuts

Here's a quick explanation of using the "IF" statement with a Regex match in Apple Shortcuts (for iOS and macOS). 

I was Googling around for this and couldn't find an exact solution for this but luckily found one that turned out much easier than I expected. The TL;DR is: put the IF statement after the regex match

Shortcut with "IF" statement after the regex based "MATCH TEXT"


Background

Here's how it works. For example, I have a Shortcut that performs some regex replacements on whatever text is in the clipboard. But I was finding some errors in the resulting data and realized that IF the clipboard text contained certain instances of text (matchable by a regular expression), I need to stop the process and manually revise those portions before continuing. 

So I tried to use an IF statement with the powerful Match Text shortcut action. But it wasn't working because I was thinking "If... this regex matches... then..." but the correct shortcut syntax isn't like that.

I Googled and searched Reddit for similar cases, but found no solutions. Funnily enough, ChatGPT was able to explain it to me. I'll reprint it's explanation here.

ChatGPT's answer

In Apple Shortcuts, you can create a workflow that performs an action if the clipboard matches a certain regex (regular expression) pattern by using a combination of the following actions:

  1. Get Clipboard: To retrieve the current content of the clipboard.
  2. Match Text: To match the clipboard content against a regular expression.
  3. If: To define the condition for what should happen if the regex matches.

Here’s a simple summation of how you can build the workflow step-by-step:

  1. Get Clipboard
  2. Match Text (with your regex)
  3. If (Match Text has any value)
    • Then (Action if regex matches)
    • Otherwise (Optional action if no match)

This will allow you to build a shortcut that conditionally runs actions based on whether the clipboard content matches your regular expression!



I gave this a try, and it worked! The important point was using the "IF" statement on a previous MATCH TEXT action, where if a match is found it will have a value, and if no match is found it will have no value. So you can set it accordingly to "If Matches has any value".

See how I arranged the shortcut as it appears in the image that goes with this post. 

Shortcut overview in text

Or written out in text form it would look like:

GET CLIPBOARD

MATCH [{desired regex}] in [Clipboard]

IF [Matches] [has any value]

SHOW ALERT [{Your match is found, so stopping this shortcut}]

OTHERWISE

{...}


Final thought

I'm really starting to get a lot of use out of custom Shortcuts to do a variety of tasks quickly with minimal keystrokes. So glad Apple replaced the old Automator with this new format. They are great for running quick regex replacements that you need frequently. Lots of other stuff too. I also use a Shortcut to connect to my Google Home speaker, set the volume, launch my Podcasts, and automatically disconnect from the speaker after X minutes. 

Happy shortcutting. 

Comments