Delete individual line breaks, save "empty" lines

Say I have text similar to the following text selected with a cursor:

This is a test. 
This 
is a test.

This is a test. 
This is a 
test.

I would like to convert it to:

This is a test. This is a test

This is a test. This is a test

In other words, I would like to replace line breaks with spaces, leaving only empty lines.

I thought something like the following would work:

RemoveSingleLineBreaks()
{
  ClipSaved := ClipboardAll
  Clipboard =
  send ^c
  Clipboard := RegExReplace(Clipboard, "([^(\R)])(\R)([^(\R)])", "$1$3")    
  send ^v
  Clipboard := ClipSaved
  ClipSaved = 
}

But this is not so. If I applied it to the text above, it will give:

This is a test. This is a test.
This is a test. This is a test.

which also removed the "empty line" in the middle. This is not what I want.

To clarify: In an empty line, I mean any line with white characters (for example, tabs or spaces)

Any thoughts how to do this?

+5
source share
4 answers
RegExReplace(Clipboard, "([^\r\n])\R([^\r\n])", "$1$2")

, CR, LF (, CR, LF, CR+LF, LF+CR). .

\R:

\ R - "R" []

, CR LF.


: "" (, )

RegExReplace(Clipboard, "(\S.*?)\R(.*?\S)", "$1$2")

, , . , , , (*?), , , . .


, .

, :

RegExReplace(Clipboard, "(\S.*?)\R(.*?\S)", "$1 $2")

.


lookbehinds lookaheads:


:

RegExReplace(Clipboard, "(?<=[^\r\n\t ][^\r\n])\R(?=[^\r\n][^\r\n\t ])", "")


:

RegExReplace(Clipboard, "(?<=[^\r\n\t ][^\r\n])\R(?=[^\r\n][^\r\n\t ])", " ")

- \S, , lookbehinds lookaheads. , .

+4
Clipboard := RegExReplace(Clipboard, "(\S+)\R", "$1 ")
+1

, :

text=
(
This is a test. 
This 
is a test.

This is a test. 
This is a 
test.
)
MsgBox %    RegExReplace(text,"\S\K\v(?=\S)",A_Space)
+1
source
#SingleInstance force

#v::
    Send ^c
    ClipWait
    ClipSaved = %clipboard%

    Loop
    {
        StringReplace, ClipSaved, ClipSaved, `r`n`r`n, `r`n, UseErrorLevel
        if ErrorLevel = 0  ; No more replacements needed.
            break
    }
    Clipboard := ClipSaved
    return
+1
source

All Articles