I have been trying to use RegEx to search and replace the duplicates of a list but with my limited knowledge of RegEx, I'm not sure if it is possible. I need to keep this formatted this way as it keeps the proper spacing when pasting in a Google Sheet. The copied text is constantly changing so I can't use a set Search for (OH) or (IMAGE). I appreciate any help and let me know if any clarification is needed.
Copied Text in Variable
OH
OH
OH
OH
OH
OH
IMAGE
IMAGE
IMAGE
IMAGE
IMAGE
IMAGE
You might try this. You could do this in BBEdit which is a text editor that you should own IMO. BBEdit supports RegEx Find and Replace.
This approach requires applying three search and replace patterns in RegEx sequentially.
First, settle upon a character that does not normally occur in your text. For the sake of an example, let's assume that "@" does not occur in the text you are trying to deal with. I will use "@" in the proposal below.
Find: ^(.*)$\n^(\1)
Replace: \1\n@\1
Find: ^(@?(.*))$\n^(\2)
Replace: \1\n@\2
Find: ^@.*
Replace: %return%
I will not try and argue that this is the most elegant way to deal with the task at hand. But in my trials, it seems to work. Depending on how often you have to do this task, this approach might be sufficient.
Hi @theandouz,
Since I've done something similar to this in the past I'll give you a purely KM way of solving your problem without the use of regex or any other programming language or tool.
You'll need to enable it and set up a trigger for it (if you want). I also don't use the clipboard simply as it makes testing easier. Instead I keep the text to be processed in the variable LocalInputText and the output goes into the variable LocalOutputText. I use a Display Text action to show the end result.
This is what the macro looks like:
and the output from it looks like this:
I hope this helps - obviously you can change it to suit your needs.
Thank you! This is great I'm just running into a weird issue on my end where this macro works on my laptop (running KM 9) but not on my desktop (running KM 8). Really weird but I'm gonna try and see if something is conflicting
@theandouz, I realize that if you are not familiar with JavaScript and JXA, then that might not be as simple as I implied.
So, here's a complete Macro that performs this task.
You can use the JXA script it contains as a reusable script. All you need to do is set a KM Variable "Local__SourceStr" to your data, and call the script.
Thanks JMichaelTX! Yea I have been trying to learn a bit of JavaScript and JXA for macros when needed but can be challenging. Thanks for your help with the script!