Advanced Find and Replace

Hi Everyone!

I am new to KM and and have already found it extremelly useful to change some xml code but have come across something that is more advanced and I have no idea how to create an action to sort it!

Is there anyway that KM can find a word in a string and copy that word to another place in the string?

Here is an example:

<hotspot name="xxxxx" style="hs_circle" ath="150.712" atv="13.351" linkedscene="landing2" tag="Landing" />

What I need to do is to automatically take the word landing2 (or whatever word it may be as it will change) and copy to paste it in place of xxxxx

So once done it will look like this:

<hotspot name="landing2" style="hs_circle" ath="150.712" atv="13.351" linkedscene="landing2" tag="Landing" />

So I guess I am asking if there is anyway to find the word before the word tag and copy / paste it into the part that says xxxxx.

Also the ath section will change and be different everytime so a simple find / replace of the whole string will not work!

Hope this makes sense!

Thank you in advance for any help :slight_smile:
Margate

This seems to work on your sample text.

Regex Test Example.kmmacros (3.3 KB)

Keyboard Maestro Export

4 Likes

Thank you for your help, yes it will work but because the string changes every time it will only work with this scenario. So the ath="150.712" can be any number at all so searching for a specific string will not work with anything else. :neutral_face:

The search scheme in the Regular Expression doesn't reference that data. It works regardless of the specific data, looking just at the name and linkedscene tags. Throw a different line in the Text box to confirm.

Here's a link to a regex101.com page showing the example. It includes a description of how the regex works:

https://regex101.com/r/D4z2bg/1

Ah, I see now. I seriously need to up my coding skills! I have tried loads of different combinations and they all work, thank you very much for your help. :slight_smile:

Brilliant, I had no idea about regular expressions! I will take some time and learn them as I can see that they will actually save me a ton of time when I know how to use them as I am already thinking of numerous ways they will save me time... :grinning:

Yes, it will take a LONG time to learn them, but they're worth it. Good luck!

1 Like

Basic regular expressions are not that difficult to learn. It depends upon your level of talent and even more on how much time and effort you put in.

I recommend BBEdit as a test-bed.

I recommend BBEdit because…
  • Although BBEdit is a commercial product, the trial version reverts to a
    still very powerful and free (BBEdit-Lite) version after 30 days.
  • It is very Mac-like.
  • It has excellent support for RegEx.
    • Including a regex-playgound in the commercial version.
  • It is very AppleScriptable.
  • It has a two versatile command line tools.
  • It has been in continuous development for over 25 years.

The only downside to BBEdit for learning regex to use with Keyboard Maestro is that it uses PCRE regex whereas KM uses ICU regex.

The fact that there are different “flavors” of regular expressions is confusing to most users at least at first.

A useful alternative to BBEdit is CotEditor. It's free and uses ICU regex, although it's not nearly as powerful as BBEdit (even the free lite version).

Regular Expressions are enormously useful, and I find them quite fun (like puzzles).

Enjoy the journey.

5 Likes

I would add to that: regex101.com is my go-to for creating and testing regular expressions. It tells you if something's wrong, and gives an explanation of how each part of the regular expression works. And it's free.

2 Likes

Brilliant, thank you for your help.

1 Like

Thank you for your help, I used to code in BBEdit years ago so am familiar with the program. Never did get good at code though unfortunately!

1 Like