如何使用 Java 的 String.replaceAll 方法替换加号字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/610333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:50:36  来源:igfitidea点击:

How to replace a plus character using Java's String.replaceAll method

javaregex

提问by John Topley

What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAllmethod in the String class? I can't get the syntax right.

replaceAll对于 String 类中的Java方法,将加号 (+) 作为第一个参数(即要替换的字符串)的正确正则表达式是什么?我的语法不正确。

采纳答案by toolkit

You need to escape the +for the regular expression, using \.

您需要+使用 对正则表达式进行转义\

However, Java uses a String parameter to construct regular expressions, which uses \for its own escape sequences. So you have to escape the \itself:

但是,Java 使用 String 参数来构造正则表达式,该表达式\用于自己的转义序列。所以你必须逃避它\本身:

"\+"

回答by Kris

You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.

您需要用\ 对+ 进行转义,并且因为\ 本身是Java 字符串中的一个特殊字符,所以您需要用另一个\ 对其进行转义。

So your regex string will be defined as "\\+" in Java code.

因此,您的正则表达式字符串将在 Java 代码中定义为“\\+”。

I.e. this example:

即这个例子:

String test = "ABCD+EFGH";
test = test.replaceAll("\+", "-");
System.out.println(test);

回答by james

when in doubt, let java do the work for you:

如有疑问,让 java 为您完成工作:

myStr.replaceAll(Pattern.quote("+"), replaceStr);

回答by Simon Nickerson

If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:

如果您想要一个简单的字符串查找和替换(即您不需要正则表达式),使用Apache Commons 中StringUtils可能更简单,它允许您编写:

mystr = StringUtils.replace(mystr, "+", "plus");

回答by codaddict

Others have already stated the correct method of:

其他人已经说明了正确的方法:

  1. Escaping the +as \\+
  2. Using the Pattern.quotemethod which escapes all the regex meta-characters.
  1. 逃避+作为\\+
  2. 使用Pattern.quote转义所有正则表达式元字符的方法。

Another method that you can use is to put the +in a character class. Many of the regex meta characters (., *, +among many others) are treated literally in the character class.

您可以使用的另一种方法是将 放入+字符类中。许多正则表达式元字符的(.*+其中许多人)是从字面上的字符类处理。

So you can also do:

所以你也可以这样做:

orgStr.replaceAll("[+]",replaceStr);

Ideone Link

链接

回答by Ramnath

Say you want to replace -with \\\-, use:

假设您要替换-\\\-,请使用:

 text.replaceAll("-", "\\\\-");

回答by JDGuide

String str="Hello+Hello";   
str=str.replaceAll("\+","-");
System.out.println(str);

OR

或者

String str="Hello+Hello";   
str=str.replace(Pattern.quote(str),"_");
System.out.println(str);

回答by Jay

How about replacing multiple ‘+' with an undefined amount of repeats?

如何用未定义的重复次数替换多个“+”?

Example: test+test+test+1234

示例:测试+测试+测试+1234

(+) or [+] seem to pick on a single literal character but on repeats.

(+) 或 [+] 似乎选择单个文字字符但重复。