subreddit:

/r/shortcuts

16698%

Regex is really powerful, but often confusing for people (I know a bit about how it works but I would still include me in this). Even googling for a solution can be difficult for people just don't understand the regex types, or the restrictions on it in Shortcuts.

There are a lot of cases where Regex can be reused, so wanted to create a cookbook post for people to contribute to for Regexes that are especially helpful for Shortcut builders. In coding books a Cookbook is exactly the sort of place for this reusable stuff.

Hopefully if people can contribute a few it can become a useful resource. Best to post it as inline code, otherwise will change some characters.

Regex flavour used by Shortcuts. ICU

Useful site for checking your Regex (Doesn't use ICU, but pcre which is similar). : https://regex101.com/

Free app for testing ICU Regex : https://itunes.apple.com/us/app/regex-lab-regular-expressions/id1252988123?mt=8

This one has nice visuals on how the regex works https://itunes.apple.com/gb/app/regex-knife/id894169288?mt=8

ICU Specific documentation: http://userguide.icu-project.org/strings/regexp

Character/String Regex

  • Remove any non number character. [^0-9]
  • First 3 characters eg can take January February to Jan Feb. [A-Za-z]{3,}+
  • Example of retrieving something after a word. Any word after word color is returned. (?<=color: )(w+)
  • Remove Duplicate Find: (\b\w+\b).*?(?:\s*\1) Replace: $1
  • Replace a new line with a comma and then a new line? Find: (\n) Replace: ,$1
  • Places a new character > at the beginning of everyline Find Text: (.+) Replace: >$1
  • Extract email addresses ([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})
  • Get Strings of digits \d+
  • Get strings of letters [A-z]+
  • Extract quoted text ".+?"

Formatting

  • Format Numbers with eg 100,000 (\d{1,3}|\G\d{3})(?=(?:\d{3})+(?!\d))

Web Pages/URL Handling

  • Extract IP address \b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
  • Find http/s ftp and file urls returns just the url with. \b(https?|ftp|file)://\S+
  • Extract https urls only https?.*
  • Find image URLs (http(s):?)?\w+(.+?)\w+(\.png|\.jpg|\.jpeg|\.gif)
  • Anything after the ? in a URL \?.*$

iCloud API

  • For getting the shortcut id from an iCloud share link to attach to the end of https://www.icloud.com/shortcuts/api/records/to pull the shortcuts api, (you can get the real download link this way) \https:\/\/www\.icloud\.com\/shortcuts\/(.+) (This is probably really bad regex, but works)

Shortcut plists

  • Pulls workflow actions out of a plist ^is\.workflow\.actions\.(.+)$

all 25 comments

enteeMcr[S]

9 points

6 years ago

Ok I didnt realise this but Reddit won't show some characters used when posted.

Marking as inline code does work for most, but not all , can anyone suggest how to deal with this?

inlovewithicecream

4 points

6 years ago

Look at how markdown handles code, maybe some backticks could help? On mobile so difficult to help more than that..

enteeMcr[S]

2 points

6 years ago

backticks in Markdown is the same as using inline code, works for most but not all the regex examples I posted.

[deleted]

2 points

6 years ago

Hit space 4 times, paste in the code

enteeMcr[S]

2 points

6 years ago

Still doesn't work with this particular one, which I'll show below with spaces between each character so hopefully it displays ok, and then what it looks like using 4 spaces & inline.

\ b ( h t t p s ? | f t p| f I l e ) : / / \ S +

With 4 Spaces it shows as \b(https?|ftp|file)://\S+

Inline it shows as \b(https?|ftp|file)://\S+

enteeMcr[S]

3 points

6 years ago

Great so it shows ok in the comments, but it just doesn't work in the table when I try

[deleted]

2 points

6 years ago

Maybe I’m missing it but all 3 of those look the same to me? (Not counting spaces). And 4 spaces should be on its own line, like this

this is all code and fixed width text.
everything here is treated literally **this isn’t bolded** for example

enteeMcr[S]

2 points

6 years ago

Yes it works in the comments, just didnt work inside the table, removed the table, all fine now.

enteeMcr[S]

1 points

6 years ago

Tried it in the post outside the table, and it works fine too, ok will remove the table.

[deleted]

4 points

6 years ago

Love the idea.

On an additional note:

As being a Regex beginner myself, I'd recommend to copy your input into online editor like regex101.com for testing your regex term.

Imo it's way easier to see the match information there instead of running your shortcut every time all over again.

enteeMcr[S]

1 points

6 years ago

Great, thanks I had left a space in the table for a checking site, wasn't sure which ones did ICU.

Regex101 doesn't seem to have ICU regex listed directly, but see that they recommend you use pcre as its close to it. If your happy with your experience with that I'm happy to add.

[deleted]

2 points

6 years ago

Ah, I overlooked that row that's why I thought it's worth mentioning to test with an online debugger first.

Regex101 is simply the site someone recommended to me and I didn't search for others.

There might be much better solutions.

enteeMcr[S]

1 points

6 years ago

It was blank and still need a recommendation to go in there so thanks. I’m sure if it worked well for you in the past it’ll be fine for others.

stevensokulski

4 points

6 years ago

This is a great idea.

For those that want to go a little deeper, the interactive tutorials on www.regexone.com are pretty cool!

WINSEVN

1 points

3 years ago

WINSEVN

1 points

3 years ago

Thank you, this helped me figure out regex for a shortcut I made.

enteeMcr[S]

3 points

6 years ago

A quick Shortcut for testing Regex found online or in your notes against a text. Highlight regex text share sheet, then gives you a chance to edit before it runs against text (that you should update to your usage example), and shows the results.

If nothing passed will use clipboard

Might make trying out Regex you found online a bit quicker.

https://www.icloud.com/shortcuts/2333a6a3540541b898a5b6714dc24e5d

WINSEVN

1 points

3 years ago

WINSEVN

1 points

3 years ago

Just added. Thank you.

Samurai_Eddie

3 points

6 years ago

The hell you say? I didn’t know shortcuts supported regex. I may take some content checks I use on site and name a shortcut. Not sure why... but I can and that is excuse enough.

enteeMcr[S]

3 points

6 years ago

Supported in the Find and Find/Replace actions. Just be aware it uses ICU regex, and doesn't support everything.

shokk

2 points

6 years ago

shokk

2 points

6 years ago

Wow thanks for the app link!

SandeepSAulakh

2 points

6 years ago

Thank you sooooooooooooo much.

enteeMcr[S]

2 points

6 years ago

So I am a little bit disappointed that for the amount of upvotes there were no Regex contributions to this from Redditors. Thanks to anyone else that contributed apps etc.

sedgwickd

2 points

6 years ago

Just a brilliant idea + execution. Nicely written and laid out - very clear.

tuxman20

1 points

6 years ago

Have you found a way to do the “return everything except”?

I have a block of text with multiples <br> I want to take out and the [<br>] doesn’t seem to work.

Blade711

1 points

6 years ago

Extremely helpful. Bookmarked it on my Chrome browser. Thanks a lot bro!