Need Help Getting a RegEx Search to Work with a Variable

I'm trying to parse the email address out of the %MailSender% variable. An example value for %MailSender% is:

BevMo <bevmo@em.bevmo.com>

The regular expression I'm using is:

(?<=\<)(.*?)(?=\>)

I've tested the expression using BBEdit (a regex-capable app) and also using https://regex101.com. Both return the expected value of bevmo@em.bevmo.com. But when I try this in KM it returns the error: Macro Cancelled Search Regular Expression failed to match (?<=\<)(.*?)(?=\>)

I'm using the action, "Search Variable "MailSender" Using Regular Expression..." ... Using Regular Expression (having typed MailSender in (without the % marks) as the variable name).

While trying to debug the problem, I found that if instead of using the variable I set it to search a hard-coded text string. i.e.:
Search Text "BevMo <bevmo@em.bevmo.com>" Using Regular Expression... it works!

So it seems the regex is being handled correctly, but I'm not providing the varible contents properly. Can anybody tell me what I'm doing wrong?

Update:

I found that if I use "Set System Clipboard to Text "%MailSender%"

and then so my regex search on the clipboard instead of the variable direction
i.e : Search System Clipboard Using Regular Expression..."
it works.

Does anybody know why executing the search against the variable doesn't work, but copying the variable to the clipboard and searching the clipboard does? Knowing the logic might save me a lot of time in the future ;o)

Have you tried something like this?

(\b\w+@[\w.]+\b)

Hey Lionel,

Welcome to the forum!   :smile:

That's not the problem.

This clearly works:

Extract Email Address from Mail Message v1.00.kmmacros (7.3 KB)

So it's most likely that you've slipped up somewhere.

In the future please post your work, so people don't have to guess or recreate it to test.

This is easy to do using the Keyboard Maestro Editor's Share to Forum function, although it takes a practice run (or three) to get used to how it works.

How to Post Your Macro to the Forum

If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?

-Chris

Thanks for confirming my conclusion. I was trying to operate on MailSender, which didn't work. When I assigned %MailSender% to the clipboard and operated on that, it worked fine. In your example, you assigned %MailSender% to local_TEST and operated on that -- confirming my suspicion that the problem was that I was trying to operate directly on MailSender. Your way is cleaner since it doesn't overwrite the clipboard (which may have something useful in it). I tweaked my macro based on your example. Thanks.

...

Delete All Mail from This Sender Macro (v9.2)

Delete All Mail from This Sender.kmmacros (6.3 KB)

1 Like