Extracting the Sender's Name from an Email Address

During yesterday's APP Pro Zoom Call, R.J. described a macro that, among other things, extracted the name of a person from an email address - for use as a reference in Tana.

This post describes how that was done using a regular expression. You don't need to become expert in how regular expressions work to use this technique. You can just copy the code and use it. But knowing that it is not magic may be useful for other times when a regular expression would solve a problem.

There is an important distinction between "search and replace" functionality and regular expressions. With "search and replace" you look for specific text and replace it with specific text. With a regular expression you work with patterns.

When Keyboard Maestro extracts the name of the sender of an email using the %MailSender% token, it is in the form shown by the green arrow in the screenshot. The sender's name is followed by an email address. To create a reference for use in Tana, the name must be extracted; the email address is not required.

The KM action in the screenshot uses a regular expression to parse the full name into two "capture groups" each of which is enclosed within parentheses. (Red arrow) The first capture group matches any text up to the "<" character, which the "^" operator excludes. The second capture group matches the rest of the address.

Interpretation:

([^>]+) → one or more characters until the ">" character is encountered

(.*) → zero or more characters - any character - up to the end of the line

Keyboard Maestro recognizes that there are two capture groups and provides three options for variables, (1) everything, pointed to by the green arrow, (2) the name of the sender, pointed to by the blue arrow , and (3) the email address, pointed to by the the orange arrow. Of these three options, we need to assign a variable only to the second.

That variable can then be used elsewhere in the macro, perhaps surrounded by [[]] if a Tana reference is needed.

The person used in this example is the developer of the very interesting and recently released application Whisper Memos.