java Java替换特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40673531/
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-11-03 05:24:15  来源:igfitidea点击:

Java replacing special characters

java

提问by Jammy

I'm trying to replace special characters in a file with a pattern with only special characters, but it doesn't seem to be working.

我正在尝试用仅包含特殊字符的模式替换文件中的特殊字符,但它似乎不起作用。

String special = "Something @$ great @$ that.";
special = special.replaceAll("@$", "as");

However, when I run that I get the original string instead of the replaced string. What am I doing wrong?

但是,当我运行它时,我得到的是原始字符串而不是替换后的字符串。我究竟做错了什么?

回答by Nicolas Filotto

Simply use String#replace(CharSequence target, CharSequence replacement)in your case to replace a given CharSequence, as next:

只需String#replace(CharSequence target, CharSequence replacement)在您的情况下使用来替换给定的CharSequence,如下所示:

special = special.replace("@$", "as");

Or use Pattern.quote(String s)to convert your Stringas a literal pattern String, as next:

或用于Pattern.quote(String s)将您的转换String为文字模式String,如下所示:

special = special.replaceAll(Pattern.quote("@$"), "as");

If you intend to do it very frequently, consider reusing the corresponding Patterninstance (the class Patternis thread-safe which means that you can share instances of this class) to avoid compiling your regular expression at each call which has a price in term of performances.

如果您打算非常频繁地执行此操作,请考虑重用相应的Pattern实例(该类Pattern是线程安全的,这意味着您可以共享该类的实例)以避免在每次调用时编译正则表达式,这会影响性能。

So your code could be:

所以你的代码可能是:

private static final Pattern PATTERN = Pattern.compile("@$", Pattern.LITERAL);
...
special = PATTERN.matcher(special).replaceAll("as");

回答by Mritunjay

Escape characters:-

转义字符:-

    String special = "Something @$ great @$ that.";
    special = special.replaceAll("@\$", "as");
    System.out.println(special);

For Regular Expression, below 12 characters are reserved called as metacharacters. If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.

对于正则表达式,保留 12 个以下的字符称为元字符。如果要将这些字符中的任何一个用作正则表达式中的文字,则需要使用反斜杠将它们转义。

the backslash \
the caret ^
the dollar sign $
the period or dot .
the vertical bar or pipe symbol |
the question mark ?
the asterisk or star *
the plus sign +
the opening parenthesis (
the closing parenthesis )
the opening square bracket [
and the opening curly brace {

references:- http://www.regular-expressions.info/characters.html

参考资料:- http://www.regular-expressions.info/characters.html

回答by AdamK

The method replaceAll accepts regex as a pattern to substitue: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

方法 replaceAll 接受正则表达式作为替换模式:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang .细绳)

Try simply:

简单地尝试:

special = special.replace("@$", "as");

回答by Syed Salman Hassan

String special = "Something @$ great @$ that.";

System.out.println(special.replaceAll("[@][$]", "as"));

it should be like this.

它应该是这样的。

回答by Anneli Manneli

special = special.replaceAll("\W","as"); 

works with all special characters.

适用于所有特殊字符。

回答by Robin Lack

Note that the first given parameter is not a string you want replace. It is a regular expression. You can try to build a regular expression that matches the string you want to replace on this site.

请注意,第一个给定的参数不是您要替换的字符串。它是一个正则表达式。你可以尝试建立要替换字符串相匹配的正则表达式上这个网站

special = special.replaceAll("\\@\\$", "as");would work, as suggested by @Mritunjay

special = special.replaceAll("\\@\\$", "as");会起作用,正如@Mritunjay 所建议的那样