Search for and copy text between characters

I have thousands of filenames and need to copy text between specific characters. For example, a filename might be...


Is it possible to search for "important_data", copy it, and replace the dash with a space, so I end up with: important data

I'm wondering if I can search for the specific text between "three_" and ".jpg", copy that text, and replace the dash with a space.

Then I'd simply continue the macro by pasting that text it in another location.

In general, the RegEx pattern is:

For example:

You can search for any specific text just by using the text in the RegEx pattern:

but I suspect you mean any text between two strings, as I have shown above.

You can use the Search using Regular Expression action to extract any text you want using RegEx, and set a KM Variable to it.

After you have the KM Variable from above, you can use Search and Replace action.

You can use a plain text S&R, or you can use RegEx. For your use can plain text will work fine:

Search For:

Replace with:

where 𝍖 is a SPACE character.


1 Like

Thank you so very much, Michael. I have the piece of text ("important data") assigned to a variable, but how do I then copy the data from that variable, back to the clipboard? In essence, by the time this macro ends, I'd have "important data" copied to the clipboard. I'm sure it's simple, but I'm a dummy when it comes to any kind of coding. I'm not sure what to do with the variable once I have it.

One more question: is it possible to search for any data between the last underscore within the filename and the .jpg suffix?

I tried using:
But it just searched for the first underscore it came across. Makes sense. I'm just curious if we can make this a little more efficient. The text I need will always come after the last underscore and before the jpg extension.


You can use the KM Set Clipboard to Text:

This should work:

Using "one_two_three_important-data.jpg" as the source, it will return:

in the Capture Group.

You're Awesome!!! Everything works! I'm trying not to feature creep here. What you've built for me will already save me a ton of time. Last question, I promise :stuck_out_tongue:

Can I do a search for the piece of text before the first underscore, and concatenate the 2 searches together? For instance, say I have:


and I want to end up with the following copied to the clipboard:
one important data

This is what I have right now:

OK, last answer. :wink: Please post new questions in a new topic.
Also, in the future, it would be best to present all of your requirements at the beginning.
That will save all of us a lot of time.

So, this should do it:




For details, see

Example Output


MACRO:   Extract Parts of File Name [Example]

~~~ VER: 1.0    2019-11-19 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Extract Parts of File Name [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

1 Like

THANK YOU! I'm sorry about the feature creep; I was getting more ideas as this macro took life. You're the best. :hugs:

I also appreciate the link to regex, showing me what each piece of code does. That's super helpful.


1 Like