Java 如何删除字符串中除点和逗号以外的所有特殊字符

Java 如何删除字符串中除点和逗号以外的所有特殊字符,java,android,Java,Android,我有一个包含许多特殊字符和文本的句子,我想删除除点和逗号以外的所有特殊字符 例如,这就是我们所拥有的: [u' %$HI# Jhon, $how$ are *&$%you.%$ 我试图生成以下字符串: HI Jhon, how are you. 我试过这个 ("[u' %$HI# Jhon, $how$ are *&$%you.%$").replaceAll("[^a-zA-Z]+",""); 但它也会删除逗号和点。我希望逗号和点在那里 最后我找到了解决办法: Python

我有一个包含许多特殊字符和文本的句子,我想删除除点和逗号以外的所有特殊字符

例如,这就是我们所拥有的:

[u' %$HI# Jhon, $how$ are *&$%you.%$
我试图生成以下字符串:

HI Jhon, how are you.
我试过这个

("[u' %$HI# Jhon, $how$ are *&$%you.%$").replaceAll("[^a-zA-Z]+","");
但它也会删除逗号和点。我希望逗号和点在那里

最后我找到了解决办法:

Python:

import re

my_str = "[u' %$HI# Jhon, $how$ are *&$%you.%$"
my_new_string = re.sub('[^.,a-zA-Z0-9 \n\.]', '', my_str)
print (my_new_string)
爪哇:

谢谢大家。我不知道我的问题出了什么问题,我没有提问的自由-(

你需要添加逗号和点,所有字符都在括号内,就像我刚才做的那样

你可能也希望包括数字

("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z0-9]/g, '');
编辑

而且,如下文所述,您的输出还需要空格:

("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z ]/g, '');
这也可能有助于:

>>> punctuation = """!\"#$%&'()*+-/:;<=>?@[\\]^_`{|}~"""
>>> string = "[%$HI# Jhon, $how$ are *&$%you.%$"
>>> edited = ""
>>> for i in string:
...     if i not in punctuation:
...         edited += i
...
>>> edited
'HI Jhon, how are you.'   
>>标点符号=”!\“\$%&'()*+-/:;?@[\\]^ `{124;}~”
>>>string=“[%$HI#Jhon,$WARE*&$%you.%$”
>>>编辑=“”
>>>对于字符串中的i:
…如果我不使用标点符号:
…编辑+=i
...
>>>编辑
“嗨,约翰,你好吗?”
使用lambda[java]组装一个不带“特殊”字符的新字符串


定义“特殊”。@StefanPochmann很好。我假设有任何非字母数字字符,但这也会降低有效编写问题的能力。以下编辑:为什么不在
replaceAll
中的字符类中添加点和逗号以删除不可接受的字符?可能重复
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z ]/g, '');
>>> punctuation = """!\"#$%&'()*+-/:;<=>?@[\\]^_`{|}~"""
>>> string = "[%$HI# Jhon, $how$ are *&$%you.%$"
>>> edited = ""
>>> for i in string:
...     if i not in punctuation:
...         edited += i
...
>>> edited
'HI Jhon, how are you.'   
String s = "[u' %$HI# John, $how$ are *&$%you.%$";
s.codePoints().mapToObj( Character::toChars ).filter(
    a -> (a.length == 1 && (Character.isLetterOrDigit( a[0] ) || Character.isSpaceChar( a[0] )
        || a[0] == '.' || a[0] == ',')) )
    .collect( StringBuilder::new, StringBuilder::append, StringBuilder::append ).toString();
//u HI John, how are you.