Java 从字符串中删除标点符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24395364/
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
Remove punctuation from string
提问by MariaH
I have a string and I need to remove these symbols: -- + [ ] { } ( ) \ /
我有一个字符串,我需要删除这些符号: -- + [ ] { } ( ) \ /
For example:
例如:
String clean = "This \ is / an example. This -- is + an [(example)].";
clean = clean.replaceAll("[/[()/]]", "");
clean = clean.replaceAll("/-/-", "");
clean = clean.replaceAll("\/","");
clean = clean.replaceAll("\\", " ");
clean = clean.replaceAll("\+", "");
return clean.replaceAll("[ ]+", " ").trim();
My output should be: This is an example. This is an example.
我的输出应该是: This is an example. This is an example.
My code does not remove everything I need and also I would like to know if there is a shorter way to do this.
我的代码并没有删除我需要的一切,而且我想知道是否有更短的方法来做到这一点。
--
——
Just some particularities I should mention: -
should be removed only if there are two together.
/
should be replaced by a whitespace. I'm going to try to adapt your solutions here. Thanks.
我应该提到的只是一些特殊性:-
只有当有两个在一起时才应该删除。
/
应该用空格代替。我将尝试在此处调整您的解决方案。谢谢。
采纳答案by Bobulous
You can simply call the String.replaceAll method and specify that those characters must be replaced by the empty String:
您可以简单地调用 String.replaceAll 方法并指定必须将这些字符替换为空字符串:
clean = clean.replaceAll("(?:--|[\[\]{}()+/\\])", "");
But if you need to do this many times, it's worth creating a Pattern object so that the regex does not have to be compiled repeatedly:
但是,如果您需要多次执行此操作,则值得创建一个 Pattern 对象,以便不必重复编译正则表达式:
private static final Pattern UNWANTED_SYMBOLS =
Pattern.compile("(?:--|[\[\]{}()+/\\])");
Now you can use this to create a Matcher object and use that to do the replacement:
现在您可以使用它来创建一个 Matcher 对象并使用它来进行替换:
Matcher unwantedMatcher = UNWANTED_SYMBOLS.matcher(clean);
clean = unwantedMatcher.replaceAll("");
This should be more efficient if you need to use the replacement in a loop which runs more than a few times.
如果您需要在运行多次的循环中使用替换,这应该会更有效。
回答by kjaquier
Replace /
by \\
when escaping.
逃跑时替换/
为\\
。
String clean = "This \ is / an example. This -- is + an [(example)]."; // Had to change \ to \
clean = clean.replaceAll("[\[()\]]", "");
clean = clean.replaceAll("\-\-", "");
clean = clean.replaceAll("\/","");
clean = clean.replaceAll("\\", " ");
clean = clean.replaceAll("\+", "");
回答by merlin2011
One, you do not escape using /
, you do it using \
.
一,你不能逃避 using /
,你可以使用\
.
Two, if you need to use \
, you have to double escape it to get it into the regular expression.
二,如果你需要使用\
,你必须双重转义它才能让它进入正则表达式。
Three, you can combine all the expressions into one regex.
三、您可以将所有表达式组合成一个正则表达式。
Four, you can chain calls to replaceAll()
.
四,您可以将调用链接到replaceAll()
.
public class Replace {
public static void main(String[] args) {
String clean = "This \ is / an example. This -- is + an [(example)].";
clean = clean.replaceAll("[\[()\]{}+\\\/-]", "").replaceAll(" +", " ");
System.out.println(clean.trim());
}
}
Output:
输出:
This is an example. This is an example.
回答by Kendall Frey
You should be able to remove everything in one fell swoop. Just put everything in a character class ([]
).
您应该能够一举移除所有东西。只需将所有内容都放在一个字符类 ( []
) 中。
[\[\]+{}()\/-]
As in:
如:
clean = clean.replaceAll("[\[\]+{}()\\/-]", "");
回答by M Anouti
You may try:
你可以试试:
String clean = "This \ is / an example. This -- is + an [(example)].";
return clean.replaceAll("[(--)+\[\]{}()\\/]", "").trim());