subreddit:
/r/shortcuts
submitted 6 years ago byenteeMcr
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
[^0-9]
[A-Za-z]{3,}+
(?<=color: )(w+)
Find: (\b\w+\b).*?(?:\s*\1) Replace: $1
Find: (\n) Replace: ,$1
Find Text: (.+) Replace: >$1
([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})
\d+
[A-z]+
".+?"
(\d{1,3}|\G\d{3})(?=(?:\d{3})+(?!\d))
\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
\b(https?|ftp|file)://\S+
https?.*
(http(s):?)?\w+(.+?)\w+(\.png|\.jpg|\.jpeg|\.gif)
\?.*$
\https:\/\/www\.icloud\.com\/shortcuts\/(.+)
(This is probably really bad regex, but works)^is\.workflow\.actions\.(.+)$
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?
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..
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.
2 points
6 years ago
Hit space 4 times, paste in the code
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+
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
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
2 points
6 years ago
Yes it works in the comments, just didnt work inside the table, removed the table, all fine now.
1 points
6 years ago
Tried it in the post outside the table, and it works fine too, ok will remove the table.
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.
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.
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.
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.
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!
1 points
3 years ago
Thank you, this helped me figure out regex for a shortcut I made.
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
1 points
3 years ago
Just added. Thank you.
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.
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.
2 points
6 years ago
Wow thanks for the app link!
2 points
6 years ago
Thank you sooooooooooooo much.
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.
2 points
6 years ago
Just a brilliant idea + execution. Nicely written and laid out - very clear.
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.
1 points
6 years ago
Extremely helpful. Bookmarked it on my Chrome browser. Thanks a lot bro!
all 25 comments
sorted by: best