Regex replace not working for me

Hi all

I tried a simple regex replace in VS code to replace the first white space on a line to a --

(?<=^\S*)

now I try to do the same in Keyboard Maestro but it seems to be doing nothings. here is the macro

any clue what am messing up here :smiley:

thx!

Z

You have a multi-line variable, so you need (?m) at the start of the regex to work on each line. However, you also made it more complicated than needed, I think. \S is "non-whitespace," but you could just use \s to find the whitespace directly:

(?m)^(\s)

This seems to work in my testing, replacing the captured group with two hyphens:

-rob.

1 Like

Oh wait, I misread your request as "leading whitespace" ... you just want the first whitespace, so "who am I" become "who--am I", right? If so, then I think this works:

(?m)^(.*?)\s(.*)$

-rob.

That looks... overly complicated. It'll be clearer if you can provide some sample inputs and required outputs, but to replace the first white space character of every line in a block of text try:

image

"Treating as multi-line find 0 or more non-white space characters followed by a white space character, saving the non-white space run in capture group 1. Replace that entire match with capture group 1 followed by --."

Sample input:

 first char space
middle space
nospace

Output:

--first char space
middle--space
nospace

And, in case you are wondering why your initial regex didn't work...

KM uses ICU Regular Expressions. The ICU flavor of regex does not allow unbounded lookbehinds. To quote the ICU documentation:

The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.)

So (?<=^\S*) is a no-go because of that *.

3 Likes

Stepping back (in the light of the XY Pattern) to the problem itself,
and skipping past the need for homebrew to install tree,
have you experimented with Keyboard Maestro's built-in Prompt for Folder ?

1 Like

thx all!

your help finally allowed me to solve this, for other people that may be interested here is the final macro

@ComplexPoint , not sure im following you with the select folder action, would prefer to do as much native in Keyboard Maestro :slight_smile:

thx!

Z

You appear to be:

  1. choosing a sub-folder from the directory ~/pkm
  2. copying the chosen folder path to the system clipboard

A more native approach to that might look something like: