Reputation: 1401
Apologies in advance. My question has been asked before, and answered too (Regex how to match all end of line except blank line?). I believe that the answer is not complete, with some obvious part (which unfortunately is not too obvious to me) left out. Exactly like the OP, I want to combine all contiguous non blank lines with the tab character.
Input:
AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE
FFFFFF
GGGGGG
HHHHHH
Expected output:
AAAAAA BBBBBB CCCCCC
DDDDDD EEEEEE
FFFFFF GGGGGG HHHHHH
/(?<!\s)$/mg
and (?<=[^\s])$
have been suggested as the regex to use, but what should the replacement string be?
Upvotes: 1
Views: 754
Reputation: 627190
You need to use
Find What: \S\h*\K\R(?!\R)
Replace With: \t
Regular expression: ON
Details:
\S
- a non-whitespace char (detecting a non-empty line)\h*
- 0+ horizontal whitespaces (maybe the line ends with some non-vertical whitespaces)\K
- match reset operartor discarding the text matched so far\R
- a line break (CR, LF or CRLF)(?!\R)
- not followed with another line break (avoid joining the empty line!).See the screenshot with the result:
Upvotes: 2
Reputation: 10199
You can use
(\S)\R
as the Find what part and \1\t
as the Replace withof replacement dialog.
Explanation:
\S
means NotWhitespace and \R
means lineending\1
\t
which is the tab character to get rid of the undesired linebreakUpvotes: 0