Need Help to Create Regex to Find Duplicate Returns and Remove the Last One

Need help to create regex to find duplicate returns and remove the last one...

Please help me

Input and output example ?

returns means line-breaks ?

duplicate means double-spaced, where you want single-spaced ?

First port of call:
Tip: How Do I Get The Best Answer in the Shortest Time? - Tips & Tutorials - Keyboard Maestro Discourse

Text example:

Lungs within normal limits.

Hearth has normal dimensions.

No evidence of pleural effusion.

Expected result:

Lungs within normal limits.
Hearth has normal dimensions.
No evidence of pleural effusion.

Why use regex when you can use a simpler method to remove blank lines?

Hi, thanks for your help, but it didn't work here

The line in the second box is "grep .". Did you include the space?

Yes, I did it

I found this regex solution in regex101: build, test, and debug regex

But it does not work in Keyboard Maestro

I've used "grep ." for years and it has always worked for me. So maybe if you show me your action I can fix it. I'm not eager to solve this problem with regex because that's fare more complicated that "grep .".

@Sleepy's "grep ." solution works just fine for me. And you could also replace double line-breaks with single line-breaks with a Native Keyboard Maestro search and replace Action. Search for \n\n and replace with \n

EXAMPLE Remove Duplicate Line-Breaks.kmmacros (3.1 KB)

Click to show Image of Example Macro

EXAMPLE Remove Duplicate Line-Breaks

I'm sorry but don't you want to learn something new?

I'm working very hard on something new in another thread right now. Maybe once I solved that new thing, I'll come back here and help you work on new ideas. For now, the idea that I gave you should solve your problem.

please look at my text example. it just doesnt work

EXAMPLE Remove Duplicate Line-Breaks.kmmacros (3.3 KB)


I took a look, and it appears that your text contains CARRIAGE RETURNS not LINE FEEDS. A newline character is a LINE FEED. The regex symbol you are using is for newlines, which is probably why you are failing to match.

You can probably use the KM Filter action to fix your input, or you can change your regex to search for CR's instead of newlines.

One approach:

Singly spaced lines.kmmacros (2.1 KB)

1 Like

It looks like you have a mixture of returns and line-breaks at the end of each of your lines. @ComplexPoint and @Sleepy have given you the solution now. But using the native Keyboard Maestro Search and Replace I got it to work with the example text you sent back replacing \n\r\n with \n

EXAMPLE Remove Duplicate Line-Breaks version 2.kmmacros (3.5 KB)

Click to show Example Macro