Sandeep
Sandeep

Reputation: 1401

Regex to combine non-blank lines with tab character in Notepad++

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

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

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:

enter image description here

Upvotes: 2

Lars Fischer
Lars Fischer

Reputation: 10199

You can use

  • (\S)\R as the Find what part and
  • \1\t as the Replace with

of replacement dialog.

Explanation:

  • the \S means NotWhitespace and \R means lineending
  • the parens store the last non whitespace character before a linenend into \1
  • the replacement use this and \t which is the tab character to get rid of the undesired linebreak

Upvotes: 0

Related Questions