Pulling Multiple Strings of Data From an Email Using Regex Into Separate Variables

You might have been getting stuck on the fact that [ and ] are special characters in RegEx and need to be "escaped" to match their literals. Something like this should do you:

image

...though you'll have to change Local_theText to whatever your variable holding the email's contents is called.

\[(\d+)\] has been terminated for (.*) on (\d{2}/\d{2}/\d{4})

The RegEx is longer than it needs to be, but it might be best to be overly cautious when dealing with canning notifications!

Hi @Nige_S,

Thanks for jumping in here.

I have it looking like this at the moment and it's not working. Am I doing something wrong?

Keyboard Maestro Actions.kmactions (2.1 KB)

I broke them up into 3 different tasks.
The only one that works is [(\d+)] for entity

You've accidentally added some newlines at the end of the Regex, so it's trying (and failing) to find "date and newline and newline".

Sheesh. I appreciate all the help, technical and dumb errors.

There's nothing dumb about not being able to spot invisible characters! But it is something you learn to check for after getting caught out bazillion times yourself...

2 Likes

Hey Mike,

You still don't have a solid understanding of what you're doing, and that's fine – that's how everyone starts out with regular expressions.

So what do you do to get things done? You simplify. Here's an example:

MikeS88 – Mail – Data Extraction Test v1.00.kmmacros (9.2 KB)

Macro Image

Keyboard Maestro Export

Break things down into tasks you do understand.

As you get more comfortable with those it becomes easier to stretch and reach for more understanding.

Note that in the first search action I've used the multi-line flag (?m), because I'm not assuming there will be only one line of data from the email.

From there I've broken each task into a separate action.

Pro Tip – always test your regular expressions in a good programming editor like BBEdit, but be aware that most of them don't use strict ICU regex like the macOS and Keyboard Maestro.

BBEdit uses PCRE for instance, and the differences can give newbies headaches.

I used to keep this critter around for testing, and it uses ICU regex:

I can't try out the latest version, because it requires macOS Monterey – but I suspect it's improved, since I last had my hands on it.

-Chris

1 Like

Hi Chris - I was interested in CotEditor but I too am on Mojave so I found this - the page of archived versions of the software going back to Panther!

1 Like

Hey Taj,

Splendid! Thanks.

I looked for an archive on the git page and completely missed the link on the product page.

:man_facepalming:

-Chris

1 Like

FWIW, I've posted a possible approach:

Thanks Chris, I am now reading this because I finally have the time to absorb it. I have been taking regex courses to learn some basics. This is very helpful.

1 Like

Sorry for hijacking the thread, but I have a similar but way simpler question.
I have a string: "100 Coke bottles"
Can I split it up and perform a calculation on "100" (for example *2) and then paste the answer as "200 Coke bottles"?

I have these actions I use to mark some text.
Run the macro and all numbers are incremented by one.

You can use this a template for your use case. Instead of +1 in the CALCULATE you replace it with *2.

In addition to @JimmyHartington's version, you can also use an array instead of regex (i.e. the @ComplexPoint version).

Result:

Thanks! will try.

(EDIT: This worked GREAT! Thanks a million!)

1 Like

Thanks, will try this too!

Just to be clear, the "text" portion of the input can and will vary. So I want to extract that and "print" it after the calculation. So it would look something like this:

12 Coke Bottles
24 Coke Bottles

10 Apples
20 Apples

etc

I don't really see how your solution does that?

I bet. I was just showing you another way. It was based on your example so your goalpost moved. You can use a prompt for user input to gather values as well.

Yes, thanks! Sorry for sounding rude, that was not my intention!

You weren’t rude. Hopefully, you can get to a good solution. If not, start a new topic so we can help.

KC