MatteoSp
MatteoSp

Reputation: 3048

Java regex: newline + white space

should be simple, but I'm going crazy with it.

Given a text like:

line number 1
line number 2
 line number 2A
line number 3
 line number 3A
 line number 3B
line number 4

I need the Java regex that deletes the line terminators then the new line begin with space, so that the sample text above become:

line number 1
line number 2line number 2A
line number 3line number 3Aline number 3B
line number 4

Upvotes: 16

Views: 62853

Answers (4)

Op De Cirkel
Op De Cirkel

Reputation: 29473

String res = orig.replaceAll("[\\r\\n]+\\s", "");

Upvotes: 12

gouki
gouki

Reputation: 4402

Perhaps to make it cross-platform:

String pattern = System.getProperty("line.separator") + " ";
string.replaceAll(pattern, "");

Upvotes: 3

millebii
millebii

Reputation: 1287

"\n " This is should do the trick if you are in Unix LF mode. For DOS like you need to match CRLF "\r\n ". Did check with RegexBuddy looking fine.

Upvotes: 2

dantuch
dantuch

Reputation: 9283

yourString.replaceAll("\n ", " "); this wont help?

Upvotes: 10

Related Questions