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...

one_two_three_important-data.jpg

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:
<prefix>(.+)<suffix>

For example:
three_(.+)\.jpg

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

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.

Questions?

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:
_(.+).jpg
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.

36%20PM

You can use the KM Set Clipboard to Text:
image

This should work:
_([^_]+)\.jpg

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

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:

one_two_three_imporant-data.jpg

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

This is what I have right now:
51%20PM

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:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

RegEx:

([^_]+)_.+_([^_]+)\.jpg

For details, see regex101: build, test, and debug regex

Example Output

image

MACRO:   Extract Parts of File Name [Example]

**Requires: KM 8.2.4+&nbsp;&nbsp;&nbsp;macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/0/3/03442e0e1cc7ccecb74ea7703b2a4b7eba327867.kmmacros">Extract Parts of File Name [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---


<img src="/uploads/default/original/3X/7/1/71b145993e562d4d115b2e5374cc8fa02d62a4c3.png" width="515" height="1139">
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.

Bret

1 Like