Automators 34: Getting Cozy with Regular Expressions

3 Likes

I have a question: /(bb|[^b]{2})/

6 Likes

Thanks for this episode. I’m familiar with regex and use it periodically and this ep pointed out some of the basics that i’d overlooked when I first learned regex.
There’s some good tools online if you want to see what your expression is actually doing too.
regex101.com has handy quick reference tips and will show you where and how the expression matched, even which groups.
There’s also regexr.com and regextester. com which do similar things.
Thanks again.

3 Likes

On iOS there’s RegEx Knife which let’s you store regexes and test them against your sample text.

2 Likes

I strongy feel that this should have been a video :sweat_smile:

2 Likes

I learned regex about 5 years ago, as one particular project had clear need of it. Amazingly, regex has become one of my favorite and most frequently used tools. It’s remarkable how many projects (or even simple tasks) can benefit from regex.

A simple example: Once I had to extract the names, ZIP codes, latitude/longitude coordinates, and other data from a Word file that was sent to me. The data were not at all entered cleanly and nicely (think of all those people who try to center and create columns using spaces – you know of what horror I speak). A manual process was daunting because there were about 10,000 data elements in there. Then I thought of something: Export the Word file to plain text, open that up in BBEdit, and use regex to parse and reformat the content into essentially a comma-separated value (.csv) file that I could then import into Excel or Pandas for analysis. I was done in about 10 minutes – way quicker than doing it by hand, and quicker than writing my own Python program to do it. Regex was clearly the right tool for the job.

So for all of you out there who aren’t sure whether learning regex is worth the time and effort, I definitely encourage you to give it a try. Use some sample lorem ipsum text in a text file with some numbers or symbols thrown in, for good measure, and practice searching for some of the patterns you find. Good text editors like BBEdit or Sublime Text make visualizing the regex search very easy, and you can also try online tools like regexr.com.

So give regex a try. I think you’ll be very happy that you did!

Very best regards,
– Alexander.

3 Likes

Here’s a great video resource by Corey Schafer on YouTube which shows you Regex in detail. https://youtu.be/sa-TUpSx1JA

3 Likes

Loving the love for BBEdit! It’s been an essential app for me for decades.

1 Like

I enjoyed the episode very much. Specially since I spent three hours on Sunday working on a shortcut which is essentialy a lot of RegEx.
It lets me log my listend podcast episodes from Overcast into Bear with just two taps.
https://www.icloud.com/shortcuts/62b4cd2688324d35a0ac9f10d086a7b2
Maybe it gives you some inspiration for your RegEx endeavors

1 Like

Listening to the episode right now and I’ve already got a better understanding of RegEx than I ever have before.

FYI, what I really need is a project manager to help me prioritize all the things I want to learn.

1 Like

I don’t believe this was mentioned in the episode (could be wrong?) – Expressions is a great standalone Mac app for testing regex that I’ve found helpful.

I should also mention that long time MPU favorite app TextSoap is RegEx compatible and the two of these in conjunction are extremely helpful in cleaning up or reformatting all kinds of text.

1 Like

Useful 1 page cheat sheet…

https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

1 Like

I use Expressions since it’s a part of Set App, which I have subscribed to.

Following up on the conversation regarding phone numbers. Here’s a text expander snippet that I made long ago:

var result = TextExpander.pasteboardText;
result = result.replace(/[1|\D]*(\d{3})\D*(\d{3})\D*(\d{4})\D*/,"$1-$2-$3");
TextExpander.appendOutput(result);

Basically, I copy any text with a phone number within, in any format. I don’t even have to copy JUST the phone number. There can be other text on either side. It can have a +1, a 1 or nothing. No matter how it was originally copied, it pastes just the phone number as “123-123-1234.” Very handy for pulling out junk info and pasting in a standard format.

2 Likes

There were, understandably, references to BBEdit. Just to note that Sublime Text (which is what I use) has RegEx support in its Find/Replace dialog.

I find that invaluable in testing RegExes against my data.

1 Like

i think of this as a very visual topic, and you two did a fantastic job speaking about it!