Java Regex -使用字符串的replaceAll方法替换换行

时间:2022-06-12 16:51:19

I have a string and would like to simply replace all of the newlines in it with the string " --linebreak-- ".

我有一个字符串,我想简单地用字符串“—linebreak—”替换其中的所有新行。

Would it be enough to just write:

仅仅写下:

string = string.replaceAll("\n", " --linebreak-- ");

I'm confused with the regex part of it. Do I need two slashes for the newline? Is this good enough?

我把regex部分搞混了。我需要两条斜线吗?这是足够好吗?

7 个解决方案

#1


36  

Don't use regex!. You only need a plain-text match to replace "\n".

不要使用正则表达式。您只需要一个纯文本匹配来替换“\n”。

Use replace() to replace a literal string with another:

使用replace()将文字字符串替换为另一个字符串:

string = string.replace("\n", " --linebreak-- ");

Note that replace() still replaces all occurrences, as does replaceAll() - the difference is that replaceAll() uses regex to search.

注意,replace()仍然替换所有出现的内容,replaceAll()也是如此——不同之处在于replaceAll()使用regex进行搜索。

#2


31  

Use below regex:

使用下面的正则表达式:

 s.replaceAll("\\r?\\n", " --linebreak-- ")

There's only really two newlines for UNIX and Windows OS.

UNIX和Windows操作系统只有两条新线。

#3


14  

Since Java 8 regex engine supports \R which represents any line separator (little more info: https://*.com/a/31060125/1393766).

因为Java 8 regex引擎支持表示任何行分隔符的\R(更多信息:https://*.com/a/3106012/1393766)。

So if you have access to Java 8 you can use

因此,如果您可以访问Java 8,您可以使用它

string = string.replaceAll("\\R", " --linebreak-- ");

#4


3  

No need for 2 backslashes.

不需要两个反斜杠。

 String string = "hello \n world" ;
 String str = string.replaceAll("\n", " --linebreak-- ");
 System.out.println(str);

Output = hello --linebreak-- world

输出= hello -linebreak- world。

#5


2  

Just adding this for completeness, because the 2 backslash thing is real.

只是为了完整起见,因为2反斜杠是实数。

Refer to @dasblinkenlight answer in the following SO question (talking about \t but it applies for \n as well):

请参考以下SO问题中的@dasblinkenlight答案(谈论\t但它也适用于\n):

java, regular expression, need to escape backslash in regex

java,正则表达式,需要在regex中避免反斜杠。

"There are two interpretations of escape sequences going on: first by the Java compiler, and then by the regexp engine. When Java compiler sees two slashes, it replaces them with a single slash. When there is t following a slash, Java replaces it with a tab; when there is a t following a double-slash, Java leaves it alone. However, because two slashes have been replaced by a single slash, regexp engine sees \t, and interprets it as a tab."

对转义序列有两种解释:首先由Java编译器解释,然后由regexp引擎解释。当Java编译器看到两个斜杠时,它会用一个斜杠替换它们。当有t跟随一个斜线时,Java用一个选项卡替换它;当双斜杠后面有一个t时,Java就不需要它了。但是,由于两个斜杠已经被一个斜杠替换,regexp引擎将看到\t,并将其解释为一个选项卡。

#6


1  

Looks good. You don't want 2 backslashes.

看起来不错。你不想要两个反斜杠。

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html和

#7


1  

for new line there is a property

新线路有一个属性

System.getProperty("line.separator")

Here as for your example,

举个例子,

string.replaceAll("\n", System.getProperty("line.separator"));

#1


36  

Don't use regex!. You only need a plain-text match to replace "\n".

不要使用正则表达式。您只需要一个纯文本匹配来替换“\n”。

Use replace() to replace a literal string with another:

使用replace()将文字字符串替换为另一个字符串:

string = string.replace("\n", " --linebreak-- ");

Note that replace() still replaces all occurrences, as does replaceAll() - the difference is that replaceAll() uses regex to search.

注意,replace()仍然替换所有出现的内容,replaceAll()也是如此——不同之处在于replaceAll()使用regex进行搜索。

#2


31  

Use below regex:

使用下面的正则表达式:

 s.replaceAll("\\r?\\n", " --linebreak-- ")

There's only really two newlines for UNIX and Windows OS.

UNIX和Windows操作系统只有两条新线。

#3


14  

Since Java 8 regex engine supports \R which represents any line separator (little more info: https://*.com/a/31060125/1393766).

因为Java 8 regex引擎支持表示任何行分隔符的\R(更多信息:https://*.com/a/3106012/1393766)。

So if you have access to Java 8 you can use

因此,如果您可以访问Java 8,您可以使用它

string = string.replaceAll("\\R", " --linebreak-- ");

#4


3  

No need for 2 backslashes.

不需要两个反斜杠。

 String string = "hello \n world" ;
 String str = string.replaceAll("\n", " --linebreak-- ");
 System.out.println(str);

Output = hello --linebreak-- world

输出= hello -linebreak- world。

#5


2  

Just adding this for completeness, because the 2 backslash thing is real.

只是为了完整起见,因为2反斜杠是实数。

Refer to @dasblinkenlight answer in the following SO question (talking about \t but it applies for \n as well):

请参考以下SO问题中的@dasblinkenlight答案(谈论\t但它也适用于\n):

java, regular expression, need to escape backslash in regex

java,正则表达式,需要在regex中避免反斜杠。

"There are two interpretations of escape sequences going on: first by the Java compiler, and then by the regexp engine. When Java compiler sees two slashes, it replaces them with a single slash. When there is t following a slash, Java replaces it with a tab; when there is a t following a double-slash, Java leaves it alone. However, because two slashes have been replaced by a single slash, regexp engine sees \t, and interprets it as a tab."

对转义序列有两种解释:首先由Java编译器解释,然后由regexp引擎解释。当Java编译器看到两个斜杠时,它会用一个斜杠替换它们。当有t跟随一个斜线时,Java用一个选项卡替换它;当双斜杠后面有一个t时,Java就不需要它了。但是,由于两个斜杠已经被一个斜杠替换,regexp引擎将看到\t,并将其解释为一个选项卡。

#6


1  

Looks good. You don't want 2 backslashes.

看起来不错。你不想要两个反斜杠。

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html和

#7


1  

for new line there is a property

新线路有一个属性

System.getProperty("line.separator")

Here as for your example,

举个例子,

string.replaceAll("\n", System.getProperty("line.separator"));