UPDATE: This issue has been resolved. See this post for more information.
I have the following test string:
.This sentence has two colons (::) and three slashes (///).
I want to replace the following characters with a dash ( - ):
A leading space whitespace character,
a leading dot ( . ),
all colons ( : ), and
all slashes ( / ).
With the Search and Replace action, why would a regex pattern of ^[\s.]|:|/| work, whereas the same pattern without the terminating | does not (the slashes are not replaced)?
I spent a while trying to understand this. I don't have an answer yet, but I see a small oddity. The "\s" regex string does not just represent a space. It represents a large set of characters, including a space. That may not be a problem for you, but I wanted to mention it.
Apart from that, I have another oddity. When you insert the regex manually instead of using a variable, the result seems to be different and correct:
So I'm leading in the direction that there's a bug when using the %Variable% token inside this action. Cue the defenders of KM who don't like it when someone suggests a bug.
I appreciate your efforts, and I owe you an apology for wasting your time!
Since @Airy and I had different observations, I suspected there must be something unforeseen at work. So I copied the XML for the Search and Replace action and pasted it into BBEdit.
Look what I found! There is a newline after the %Variable%local_Pattern% and before the </string>.
With the newline removed the action is working as expected.
<dict>
<key>Action</key>
<string>CaseSensitiveRegEx</string>
<key>ActionUID</key>
<integer>16720755</integer>
<key>Destination</key>
<string>Variable</string>
<key>DestinationVariable</key>
<string>local_Text</string>
<key>MacroActionType</key>
<string>SearchReplace</string>
<key>Replace</key>
<string>-</string>
<key>Search</key>
<string>%Variable%local_Pattern%
</string>
<key>Source</key>
<string>Text</string>
<key>Text</key>
<string>.This sentence has two colons (::) and three slashes (///).</string>
<key>Which</key>
<string>All</string>
</dict>
Again, thanks for your help! This forum includes so many incredibly helpful users!
In my computer science career, it took me several decades to realize that unexpected behaviour isn't really a problem, it's an opportunity for understanding (and exploiting.)
Well if you want "any whitespace" rather than "a single whitespace character", then I think you should change "\s" to "\s+".
I suspect that it wouldn't be easy to change the focus color (currently blue on my mac), but maybe the text color could be set to red like we see in the Set Variable to Calculation action when something is amiss.
I might have saved you a little time if I had trusted my intuition that the problem had something to do with end-of-line, but I really couldn't see how it could be, and my experiments with $ did not enlighten me, so I didn't mention that line of thought! Well done on having worked this one out!
This is a Mac issue, not a Keyboard Maestro issue.
The field stops you from typing a return, but it does not stop you from pasting a return, nor does it show you that the return is in the field when the field is displayed unedited.
I expect I can add a flag when editing it to show the return is in there, but that would not show when you are not editing the field.