Search With a Regular Expression (Containing a Substring in a Variable)

Hello everybody,

A quick question on KM's Search with Regular Expression I'm not able to figure out, apparently.

If this has been already solved, please let me know where to look for.

I have a string array variable with full paths and file names in Mac format, something like this:


The RegEx syntax to look for the whole path containing "myfile.txt" would be something like this:


In regex101, this pattern returns what I expect:


But the file name is actually a variable.

I get an error in KM with both patterns below:


Should I use Applescript/Javascript? What am I missing?

Any help would be highly appreciated.

Thank you!

You need to enclose the search expression with ` in the post or use the escape backslash \, otherwise, the two * are taken to be a Markdown italic code. I thought you missed the * until I quote it.

I am able to get the result correctly:

Maybe you have something else in the string, such as a linebreak?

@ccstone's advice is very good:

Hey @freewind1974,

When asking for help if at all possible please upload a β€œworking” macro that demonstrates your use-case in the minimum possible steps.

Like so:

RegEx β‡’ Extract a File Path Using a Variable in the Regular Expression v1.00.kmmacros (8.9 KB)
Keyboard Maestro Export

When posting macros to the Keyboard Maestro forum please use these guidelines:

  1. Always post a Macro File.
  2. Always post an image.

This means people won't have to reinvent the wheel to test your macro, and that significantly improves the likelihood that someone will help you.

Also a good rule-of-the-thumb is – β€œIf you're not testing you're guessing.” – and guessing quite often wastes a lot of everyone's valuable time.

The same often holds true of written descriptions of problems. They tend to leave out crucial details.

Folks generally won't download something they haven't eyeballed first, so an image of the macro is crucial.

If you haven't seen these they're worth a moment of your time:

How to Post Your Macro to the Forum

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

  • Never use %VariableName% as a variable notation, while it's legal it should NOT be – because it's the same as KM's text-token notation. If you use it then you can't tell at a glance the difference between Tokenized-Variable-Names and actual Text-Tokens.

  • is a great resource, but there are some subtle differences between the regex variants they make available and the ICU regular expression set Keyboard Maestro uses.

    • You must also pay close attention to the RegEx Flags that have been set.
  • Note that myfile.txt contains a regex metacharacter that needs to be escaped.

  • Note that this pattern ^.*myfile.txt.*$ is potentially dangerous – it can accidentally match any string in the path – not just the file name.

  • Note that multiline is NOT on by default in Keyboard Maestro, so when you're anchoring to the start of a line (not the entire string) you need to turn it ON.

    • (?m)<your-regex>

To clarify – all code posted to the forum should be properly fenced.

You can use the Pre-Formatted Text button in the forum editor: image

Which also responds to βŒ˜β‡§C

For further information please see this link:

Code Block Fencing on the Keyboard Maestro Forum and Paste AppleScript as Markdown Macro

Take Care,

(Keyboard Maestro Moderator)


Thank you so much Martin and Chris for your detailed examples and explanation... I guess I wouldn't have gotten to a solution without your help.

Your sample macro works great, Chris.

I'll follow forum guidelines better next time.

This is a great forum... thanks again!



Hi @freewind1974,

It just occurred to me, if your purpose is to get the path that contains your file, then the best way is probably not to use RegEx, because your file name may contain characters that have special meaning in RegEx.

Take this for example:

In the example above, because the file name contains brackets and - inside the brackets, it has special meaning in RegEx. A RegEx search will not find what we want. But the if variable contains the filename returns true.

If you want to get the full path that contains your file name, then you can loop through the text, like this:

Hey Martin,

My macro above has an action to escape known regex metacharacters in the file name, but your point is well taken.

Perhaps @freewind1974 will clue us in to his overall task, so we can propose alternative methods (if they're more efficient).


1 Like

Thanks for pointing that out. I did not look carefully enough.

Yes. That was my thought too.
I was thinking of either adding a break from loop action or a cancel this macro action to the macro, depending whether @freewind1974 needs a multiple match or what else he wants to do with the match result.

Hello All,

I got lost following the conversation, so I better explain what I wanted to achieve and how I did it.
It's a bit hard for me to share the macros right now... I'll try my best to explain them.

I mix and master records in Pro Tools.
I built a lot of macros inside PT: recently, I wanted to get a more intuitive (for me personally) Open Session function.
Instead of getting a "recent files" list, with very long paths (on different drives) and the session names at the end (that is, difficult to read), I thought of using a "Prompt with list" window, with just the session names (in alphabetical order), filter them the way I want (for example, typing "MIX" I get only the mixing sessions, "MST" for mastering sessions, and so on), associate the corresponding path and open the session automatically.
I jump between sessions much quicker this way.

So, in order to keep track of the sessions I progressively open, I did this:

  1. When PT's "focused window title changes", I get the session path + name from PT's Edit Window, if at the front;
    I have a couple variables, one with full session paths + names, the other with session names only. The order of course is the same.
    The entries for both variables are separated by %Return% and I already was adding a "/" on top of the path before writing here.
    If the current session is not already contained in the variables, it gets added;
  2. When closing PT, those variables are saved into txt files, and reloaded when PT reopens.
    A separate macro deletes all history (files + variables) when needed.
  3. With Option+O (my Open macro), I get prompted with the list of session names, alphabetically ordered. Then I got stuck about using RegEx with variables, and came here.
    I couldn't make @martin suggestions work; I adapted @ccstone solution and it worked perfectly.
    The path gets correctly associated.

Furthermore, once I open Pro Tools Finder with Cmd-O (regular Open Session PT command):

  1. I paste the complete session name with Shift-Cmd-G;
  2. Return + Cmd-O open the session.

Hope it's all clear now.
Thanks for the help!

Hi @freewind1974,

What we were talking is mainly about RegEx. As I said, when you have a variable in a RegEx search, make sure you understand you are searching for literal characters or you want some special characters to be interpreted by RegEx. In your case, you need to search for literal strings. @ccstone has done this for you by adding an escape character to the variable string. Other wise, a dot . in RegEx matches any character (except for line terminators). For instance, myfile.txt will not only match /path/to/myfile.txt, it will also match /path/to/myfile$txt, /path/to/myfileAtxt, /path/to/myfile-txt, etc.

From what I understand about your purpose, I think, with some modifications, the macro below should work for you.

See screenshot below for instructions.

  • To ensure the hot key is sent to your desired app, Pro Tools, it is better to use the "Send To" option. I don't have PT. So I set it to Finder. You may change it to PT. In a series of keystrokes, adding pauses might be necessary in many cases. I added an example for you.
  • The slash / in the "end with" condition is needed. Otherwise, myfile.txt will match /path/to/myfile.txt and /path/to/anycharactersmyfile.txt.

RegEx.kmmacros (5.2 KB)

Hey Lorenzo,

My eyes are glazing over...Β  :sunglasses:

I get the feeling you're doing this the hard way. Take a look at this.


Get File Name List β‡’ Return Full Path v1.00.kmmacros (10 KB)

1 Like

An aside:

Striking how often the name of a thread describes not the problem itself,
perhaps, in this case something like:

  • How to choose a filepath from a list, given only the filename ?

and instead names some small unworkable part of one possible solution.

Naming the problem itself helps everyone:

  • for the asker – unexpected and easier solutions can usually (almost invariably) be found
  • for the community as a whole – much easier search by others coming later, who are trying to solve the same problem.

Thread titles containing 'regex' or 'regular expression' seem particularly prone to this problem :slight_smile:

(as the infamous Jamie Zawinski quote might lead us to expect)