Android Java - String .replaceAll 替换特定字符(正则表达式)

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

Android Java - String .replaceAll to replace specific characters (regex)

javaandroidregex

提问by JozeRi

I need to remove some specific "special" characters and replace them with empty string if they show up. I am currently having a problem with the regex, probably with the Java escaping. I can't put them all together, it just doesn't work, I tried a lot! T_T

我需要删除一些特定的“特殊”字符,并在它们出现时用空字符串替换它们。我目前在使用正则表达式时遇到问题,可能与 Java 转义有关。我不能把它们放在一起,它就是行不通,我试了很多次!T_T

Currently I am doing it one by one which is kinda silly, but for now at least it works, like that :

目前我正在一个一个地做这有点傻,但至少现在它可以工作,就像这样:

public static String filterSpecialCharacters(String string) {
    string = string.replaceAll("-", "");
    string = string.replaceAll("\[", "");
    string = string.replaceAll("\]", "");
    string = string.replaceAll("\^", "");
    string = string.replaceAll("/", "");
    string = string.replaceAll(",", "");
    string = string.replaceAll("'", "");
    string = string.replaceAll("\*", "");
    string = string.replaceAll(":", "");
    string = string.replaceAll("\.", "");
    string = string.replaceAll("!", "");
    string = string.replaceAll(">", "");
    string = string.replaceAll("<", "");
    string = string.replaceAll("~", "");
    string = string.replaceAll("@", "");
    string = string.replaceAll("#", "");
    string = string.replaceAll("$", "");
    string = string.replaceAll("%", "");
    string = string.replaceAll("\+", "");
    string = string.replaceAll("=", "");
    string = string.replaceAll("\?", "");
    string = string.replaceAll("|", "");
    string = string.replaceAll("\"", "");
    string = string.replaceAll("\\", "");
    string = string.replaceAll("\)", "");
    string = string.replaceAll("\(", "");
    return string;
}

Those are all the character I need to remove:

这些是我需要删除的所有字符:

- [ ] ^ / , ' * : . ! > < ~ @ # $ % + = ? | " \ ) (

I am clearly missing something, I can't figure out how to put it all in one line. Help?

我显然遗漏了一些东西,我不知道如何将它们全部放在一行中。帮助?

采纳答案by Wiktor Stribi?ew

Your code does not work in fact because .replaceAll("$", "")replaces an end of string with empty string. To replace a literal $, you need to escape it. Same issue is with the pipe symbol removal.

您的代码实际上不起作用,因为.replaceAll("$", "")用空字符串替换了字符串的结尾。要替换文字$,您需要对其进行转义。同样的问题是删除管道符号。

All you need to do is to put the characters you need to replace into a character class and apply the +quantifier for better performance, like this:

您需要做的就是将需要替换的字符放入字符类中并应用+量词以获得更好的性能,如下所示:

string = string.replaceAll("[-\[\]^/,'*:.!><~@#$%+=?|\"\\()]+", "");

Note that inside a character class, most "special regex metacharacters" lose their special status, you only have to escape [, ], \, a hyphen (if it is not at the start/end of the character class), and a ^(if it is the first symbol in the "positive" character class).

请注意,在字符类中,大多数“特殊正则表达式元字符”失去了它们的特殊状态,您只需转义[, ], \, 连字符(如果它不在字符类的开头/结尾)和 a ^(如果它是“正”字符类中的第一个符号)。

DEMO:

演示

String s = "-[]^/,'*:.!><~@#$%+=?|\"\()TEXT";
s = s.replaceAll("[-\[\]^/,'*:.!><~@#$%+=?|\"\\()]+", "");
System.out.println(s); // => TEXT

回答by Shree Krishna

Use these codes

使用这些代码

String REGEX = "YOUR_REGEX";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(yourString);
yourString = m.replaceAll("");

UPDATE :

更新 :

Your REGEX looks something like

你的正则表达式看起来像

String REGEX = "-|\[|\]|\^|\/|,|'|\*|\:|\.|!|>|<|\~|@|#|\$|%|\+|=\?|\||\\|\\\\|\)|\(";

SAPMLE :

SAPMLE :

String yourString = "#My (name) -is @someth\ing"";
//Use Above codes
Log.d("yourString",yourString);

OUTPUT

输出

enter image description here

在此处输入图片说明