Trying to get rid of blank lines with regex..works in VScode not in Keyboard Maestro

Hi

i tried the following regex ^\s*\r?\n in Vscode to select all empty lines and delete them which works great. for whatever reason i cant get it to work in mu Keyboard Maestro macro:

anyone know what im doing wronmg here?

best

Z

I have no idea what VScode is, but I know a lot of ways to remove empty lines.

First, you didn't show us the setting on the cogwheel of the Search and Replace action. You have to choose between All, First or Last. Which did you pick? If you had uploaded your macro we could see. Since you didn't upload, I can't tell if this is your problem.

On a minor note, why are using \r and \n when you could use \v which matches either one. (As far as I can read in the docs.) If you use \v\v it should match CR and LF, which is I think what you're trying to do. Or maybe you can do better with \v*. In fact, if you really need to handle multiple kinds of line endings, you may need to use the KM action Filter which lets you convert lines with different kinds of newline endings.

If the goal is to remove empty lines that include spaces, perhaps your best bet is:

If your goal is to remove empty lines, perhaps your best bet is:

It can probably also be done with regex, perhaps like the following action, but why make life difficult for yourself? (This one will include the deletion of lines with white spaces.)

By the way, \s includes a set of five characters, not just spaces. Those extra five characters include \r and \n, which might be why your regex isn't working.

By default, ^ matches at the start of the text only.

As shown in the ICU Regular Expression Reference (in the Help menu)

Control the behavior of “^” and “$” in a pattern. By default these will only match at the start and end, respectively, of the input text. If the m flag is set, “^” and “$” will also match at the start and end of each line within the input text

So you basically want (?m)^\R

\R will match any line ending, including \n, \r, or \r\n.

1 Like

You are simply embedding the same regular expression in two quite different contexts, which apply it in different ways.

VSCode search and replace applies the offered regex separately and repeatedly to each line in the file buffer (every distinct line start matched afresh to ^ on your behalf).

The Keyboard Maestro action applies the regex once to the string which you offer. (One match of ^).

This gives you the choice of starting with (?m) if you want to.

thx everyone!

super helpful and greatly appreciated!!

Z