I am trying to do a regex search/replace on all hits in my clipboard. I saw in the docs that I need to do a forEach action, which I've tried, but it's still not working right.
I'm using KM 9.0.6.
Here's a simple example: I want to remove the '- ' from the front of all lines of text. i.e., I want to transform this:
Into this:
But what's happening is that only the first match is getting fixed, like so:
Your example of results ADDS an extra line after line 1. Is that intentional?
Assuming all you want to do is replace the leading "- " on each line with nothing, then that is quite easy.
You do NOT need a For Each Action. Just one Search and Replace action:
ACTION: Search and Replace Using RegEx Action (v9.1d1)
Download
Search and Replace.kmactions (551 B) NOTE: Before you import this ACTION, open the Macro and select the Action after which you want this action.
Double-clicking or Importing an Action will insert the Action just below the selected Action in the current Macro being edited.
In terms of getting the For Each approach to work:
The point to remember is that at each stage, the forEach variable just contains a copy of that one line.
It's not a reference to the original line (just a separate copy), and no changes you make to the content of that variable will be copied back to the source line.
All you need to do is:
start the process with an additional accumulator variable which is initially empty
with each forEach line, append the modified copy, followed by a linefeed, to the accumulator.
at the end, when the bullet-stripped accumulator is built, update the clipboard from it.
Thank you so much, @ComplexPoint and @JMichaelTX! Solved. This is very helpful. I went with the simple regex using \m but seeing how a more complex forEach works was also useful.