Storing a RegEx Pattern in a Variable

Hi all, I wonder is there a simple way to loop through a list of Regex patterns (about 100) and match against one line of string. The scenarios is to search an email subject for the server/application type and assign a different value to a static variable according to the type. Some examples of server/application types are:

... about 100 types

Currently I am defining them item by item using switch/case control flow. This method is quite tedious and error prone. The macro created can be very lengthy and difficult to read.

The ideal method will be storing the regex pattern and value in a table, looping this table and matching the string with the pattern. If a match is found, then assign the value. If I am not wrong, this will require storing regex pattern into a variable, but is this possible for variable in KM?

Any suggestions/advice is greatly appreciated. Thank you very much

If I understand your desired workflow correctly, then yes, there's a way to do what you want. You could create a KM dictionary that uses regex patterns for the names and values for the, uh, value, then use the For Each action to loop through every pattern in the dictionary and assign the corresponding value to the static variable once a match is found. You could also do something similar with a simple tab-separated text file that lists the regex patterns on the left and their corresponding values on the right, then use For Each to search for the pattern in each line and assign the value in the same line once a match is found.

Which of these methods sounds better to you? Whichever one you decide to go with, if you're not quite sure how to actually go about putting together a macro that would behave this way, feel free to report back with examples of the sort of patterns you would want to loop through, and one of us should be able to help you write it. The more data and details you can provide, the better. I recommend reading Tip: How Do I Get The Best Answer in the Shortest Time? and following its advice for best results.

Text processing is one of my fav topics. GG says he knows some ways. I don't want to step on his toes but I love this topic.

I don't think the problem is fully defined yet. For example you don't say what will happen if the subject line contains multiple copies of the search values (same or different), or none at all. In order to find the best solution, I need to know what you want to do in all situations. My goal is to get a full solution in a single action with no loops or switch statements, but to do that I need to understand the problem better. Can you perhaps show more details, perhaps including some examples, including the tricky cases like multiple occurrences of the search names?

Hey Johnny,

Welcome to the Keyboard Maestro forum.  :smile:

What mail client?

How are you extracting the mail subject?

Please give an example of both the search value and the return value.

If I understand your task correctly then I think the easiest and most maintainable method of doing this will be AppleScript.

It also appears that you don't need a regular expression but can use a simple literal comparison.

property LF : linefeed

set lookupTable to text 2 thru -2 of "
Dell1DB		Dell1DB_ReturnValue
Dell2DB		Dell2DB_ReturnValue
HP1DB		HP1DB_ReturnValue
HP2VPN		HP2VPN_ReturnValue
HP3CLT		HP3CLT_ReturnValue

set dummySubject to "This is my subject (Dell1DB)"

set AppleScript's text item delimiters to LF
set lookupTable to text items of lookupTable
set AppleScript's text item delimiters to tab

repeat with lineItem in lookupTable
   set lookupStr to text item 1 of lineItem
   if dummySubject contains lookupStr then
      return last text item of lineItem
   end if
end repeat

For a 100 item table this runs in about 0.02 seconds on my elderly MacBook Pro.

You can put the table in a text file for easy editing.


Hey Johnny,

Please also provide a real-world example of the email subject.


Hey Guys,

Thanks for the info and replies. My apology if I have not explained my problem clearly enough.

@ccstone, thanks for the sample AppleScript. I am using Gmail via browser and extracting one subject at a time via copy and paste. I will prepare some screenshots on my requirement and update this thread again.


I like helping people but I'm departing soon on a vacation so I can't help on this thread for a while.

Hi all, after some testing, I found that KM does support storing regex pattern in a variable. Well done KM! :slight_smile: My problem is resolved and KM has made my day. Thank you again for the good information and helps @gglick @ccstone @Sleepy.

I have attached screenshots on how I configure in KM.

1 Like