Hello, @ravivarma-kv, @terry-r and All,
I found out a simple regex, which needs to be executed one time, only ! I just assume, as Terry did, that pure blank lines are deleted, as well !
So, given, for instance, this sample text below :
1st line with 1 LEADING space
This is
a text
which does
NOT have any
LEADING space
2nd line with 1 LEADING space
A single line WITHOUT leading space
3rd line with 1 LEADING space
2nd multi-lines text
with NO
Leading space
4th line with 1 LEADING space
1st line with 3 LEADING space
5th line with 1 LEADING space
6th line with 1 LEADING space
3rd multi-lines text
WITHOUT any
leading space
A last line with 1 LEADING space
This is a
very long
multi-lines text
with
NO leading
space
for all
the
lines !
Use the following regex S/R :
SEARCH \R(?=(\x20|\z)|)
REPLACE ?1$0
Of course, select the Regex expression search mode. You should get the expected text :
1st line with 1 LEADING spaceThis isa textwhich doesNOT have anyLEADING space
2nd line with 1 LEADING spaceA single line WITHOUT leading space
3rd line with 1 LEADING space2nd multi-lines textwith NOLeading space
4th line with 1 LEADING space
1st line with 3 LEADING space
5th line with 1 LEADING space
6th line with 1 LEADING space3rd multi-lines textWITHOUT anyleading space
A last line with 1 LEADING spaceThis is avery longmulti-lines textwithNO leadingspacefor allthelines !
I verified that I got the same output, using Terry’s regex solution ;-))
Notes :
This regex searches for any kind of EOL characters, followed by, either :
A space character or the very end of file ( => Group 1 exists )
Any kind of character ( other standard char or EOL char, due the empty alternative |)
Due to the conditional replacement syntax ?1$0, this matched EOL character is rewritten ( $0 = Whole match ) only if the group 1 exists. In all the other cases, it is just deleted
Remark :
If you prefer that the joined lines are separated with a space character, we need to distinguish between the standard characters, other than a space char, beginning the subsequent lines and any other character. So, use, instead, the followed regex S/R :
SEARCH (?-s)\R(?=(\x20|\z)|(.)|)
REPLACE (?1$0)(?2\x20)
This time, due to the (?2\x20) conditional replacement, the matched EOL char is replaced with a single space character
And you’ll get the text :
1st line with 1 LEADING space This is a text which does NOT have any LEADING space
2nd line with 1 LEADING space A single line WITHOUT leading space
3rd line with 1 LEADING space 2nd multi-lines text with NO Leading space
4th line with 1 LEADING space
1st line with 3 LEADING space
5th line with 1 LEADING space
6th line with 1 LEADING space 3rd multi-lines text WITHOUT any leading space
A last line with 1 LEADING space This is a very long multi-lines text with NO leading space for all the lines !
Best Regards,
guy038