33

I'm trying to figure out how to use Regex to merge the contents of my text file

(25 lines of data) into one line.

So far, I can get Notepad++ to successfully find the lines I'm looking for by making it search for (^) , but what I'm unsure of is what is what to replace it with.

Syntax-wise I'm looking for the correct script that essentially attaches the beginning of one line to the end of the previous one. Can anyone help? Thanks

aelor
  • 10,892
  • 3
  • 32
  • 48
user3153443
  • 511
  • 3
  • 6
  • 13

5 Answers5

59

Find \R and replace with empty string.

\R matches multiple linebreak styles, including most common \r\n and \n.

Search Mode must be set to Regular expression.

user694733
  • 15,208
  • 2
  • 42
  • 68
  • In notepad++ after pressing CNTL + H to get to this screen, be sure to select 'Regular Expression' in the 'Search Mode' box, or this wont work! – Alan Nelson Sep 11 '17 at 19:50
  • https://community.notepad-plus-plus.org/topic/14791/how-to-make-all-data-in-one-line/2 I have found the link it worked for me!!. – Sivakumar Jun 03 '21 at 07:37
24
  1. Highlight the lines you want to join (or use Ctrl + A to select everything)
  2. Choose Edit → Line Operations → Join Lines from the menu or press Ctrl + J.

It will put in spaces automatically if necessary to prevent words from getting stuck together

As an alternative you can

press Ctrl+H

In Search Mode pick Extended

Find - \r\n Replace - leave it empty.

aelor
  • 10,892
  • 3
  • 32
  • 48
  • For Sublime Text Users it's: Edit → Lines → Join Lines OR Ctrl + J on Windows; Command + J on the Mac. I'm wondering how to achieve the same thing (to avoid words getting stuck together) with just Regex. Since I'm interested in creating automated scripts for dealing with different kinds of subtitle, transcript text files. – adamlogan Jul 13 '18 at 14:47
5

^ is an anchor, that means it does not match characters (it matches the position after a \n, or the the start of the string). So nothing to replace.

If you need to use regex (aelors answer sounds good => +1), then

 [\n\r]+

and replace with nothing or a space, according to your needs.

stema
  • 90,351
  • 20
  • 107
  • 135
3

You can replace

[\r\n]+

with an empty string (or replace \n+ if you know your newlines are \n)

Robin
  • 9,415
  • 3
  • 34
  • 45
2

You can do the following: Highlight all the lines you wish to join, then click "Ctrl" + "J"

Laurel
  • 5,965
  • 14
  • 31
  • 57