44

If I have the following text in my Eclipse editor:

Text Line 1
Text Line 2

I would like to concatenate the text into:

Text Line 1Text Line 2

My first idea was to search for carriage return character '\n' and replace it with '' to concatenate it.

I tried using the search function of Eclipse, but it does not recognize carriage return character.

Are there any other editor that can do this?

beaver
  • 523
  • 1
  • 9
  • 20
zfranciscus
  • 16,175
  • 10
  • 47
  • 53

5 Answers5

71

Eclipse does this if you:

  • turn on regular expression mode in search/replace
  • enter \R for the newline
Stefan
  • 10,010
  • 7
  • 61
  • 117
soru
  • 5,464
  • 26
  • 30
8

Just use Edit -> Find/Replace, switch on the Regular Expressions checkbox, search for \n and replace it by space.

I tried it in Eclipse 3.4 and it worked well.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
3

Short answer:

I decided to use \s++ as separator in multi-line search expressions (with regular expressions enabled) and \Qfoo\E to escape special characters if required.

Long answer:

As soru already answered, for any "Unicode linebreak sequence" a regular expression search with \R can be used.

A pure carriage return is represented by \r. Upper and lower case make a difference. \R represents any unicode linebreak sequence (for example \r\n).

I found this SO question because I wanted to search for a multi-line expression in Eclipse, including line breaks and tabs:

    @Override
    @Transient

In order to include the white spaces in my regular search expression I used (on Windows platform)

@Override\r\n\t*@Transient

Following expressions also work:

  • @Override\R\t*@Transient
  • @Override\s++@Transient

Please note that the second expression also matches @Override @Transient without a line break, which is fine for me.

Following expressions did not! work for me:

  • @Override\r\t*@Transient
  • @Override\n\t*@Transient

Explanation of some regular expressions:

  • \R represents any unicode linebreak sequence (for example \r\n)
  • \s represents any white space
  • \t represents a tab
  • * matches zero or more occurrences
  • ++ matches one ore more occurrences
  • \Q and \E escape wrapped content. Use them if your original multi line expression includes special regex characters, for example

\Q/**\E\s++\Q*\E

matches

    /**
     *

Also see:

Difference between \n and \r?

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Stefan
  • 10,010
  • 7
  • 61
  • 117
2

Most find and replace tasks in editors (at least, TextPad) have the ability to replace via a regex. If you can find this option in eclipse, then just use that.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
1

\r is the correct regular expression for carriage return. But Eclipse standard editor does not find it. So use external editor, for example notepad++

RoutesMaps.com
  • 1,628
  • 1
  • 14
  • 19
  • the \R worked for me in Eclipse and not the \r\n even though \r\n was what was in the files. In fact depending on if you have Linux, Windows, or iOS, there will be \n, \r\n, or \r – Captain Fantastic Apr 18 '18 at 15:56