Java 如何删除标点符号,但留下重音字母?
我试图从文本数据中删除标点符号,但保留重音字母。我不想将重音字母替换为英语对应字母。我不知道如何调整现有代码以允许使用更高的ascii字符Java 如何删除标点符号,但留下重音字母?,java,string,Java,String,我试图从文本数据中删除标点符号,但保留重音字母。我不想将重音字母替换为英语对应字母。我不知道如何调整现有代码以允许使用更高的ascii字符 while (input.hasNext()){ String phrase = input.nextLine(); String[] words = phrase.split(" "); for(String word: words){ String strippedInp
while (input.hasNext()){
String phrase = input.nextLine();
String[] words = phrase.split(" ");
for(String word: words){
String strippedInput = word.replaceAll("[^0-9a-zA-Z\\s]", "");
}
}
如果原始输入为:
哦,萨尔,你是谁,你是谁
预期产出应为:
O sal ou O sódio tambémécontracidiado em pacientes hipotensos
有什么想法吗?谢谢 试试这个。
Try this.
public class punctuationRemove {
//private static String punc = "[][(){},.;!?<>%]";
static StringBuilder sb = new StringBuilder();
static char[] punc = "',.;!?(){}[]<>%".toCharArray();
public static void main(String[] args){
String s = "Hello!, how are you?";
System.out.println(removePuntuation(s));
}
public static String removePuntuation(String s)
{
String tmp;
boolean fl=true;
for(int i=0;i<s.length();i++)
{
fl=true;
char strChar=s.charAt(i);
for (char badChar : punc)
{
if (badChar == strChar)
{
fl=false;
break;
}
}
if(fl)
{
sb.append(strChar);
}
}
return sb.toString();
}
}
公共类标点删除{
//私有静态字符串punc=“[[(){},.;!?%]”;
静态StringBuilder sb=新StringBuilder();
静态字符[]punc=“”,.;!?(){}[]%”。tocharray();
公共静态void main(字符串[]args){
String s=“你好,你好吗?”;
System.out.println(移除Puntuation);
}
公共静态字符串removePuntuation(字符串s)
{
串tmp;
布尔fl=真;
对于(int i=0;i来说,这可能效率低下,而且我相信这个想法可以改进,但是您可以创建一个方法,在字符串中循环,为不是标点符号的每个字符建立一个缓冲区
private String replacePunctuation(String s){
String output = "";
for(int i = 0; i < s.Length(); i++){
if(s.charAt(i) != '.' && s.charAt(i) != ',' && s.charAt(i) != '!') // Add other punctuation values you're concerned about. Perhaps the Regex class would be useful here, but I am not as familiar with it as I would like.
output += s.charAt(i);
}
}
}
专用字符串替换标点符号(字符串s){
字符串输出=”;
对于(int i=0;i
再说一次,可能不是最干净或最有效的,但这是我目前能想到的最好的方法。也许我没有抓住要点,但有点像
String text = "O sal, ou o sódio, também é contraindicado em pacientes hipotensos?";
System.out.println(text);
System.out.println(text.replaceAll("[\\?,.:!\\(\\){}\\[\\]<>%]", ""));
或者,根据你的例子
while (input.hasNext()){
String phrase = input.nextLine();
String[] words = phrase.split(" ");
for(String word: words){
String strippedInput = word.replaceAll("[\\?,.:!\\(\\){}\\[\\]<>%]", "");
}
}
while(input.hasNext()){
字符串短语=input.nextLine();
字符串[]单词=短语。拆分(“”);
for(字符串字:字){
String strippedInput=word.replaceAll(“[\\?,.:!\\(\\){}\\[\]%]”,“”);
}
}
考虑使用,因为“A-Z”非常以英语为中心,甚至不能处理所发现的口音
例如,除了“任何字母、任何语言”(\p{L}
)或(\s
)之外,以下内容将替换所有内容,包括标点符号。如果需要保留数字,请将其作为附加排除项重新添加
replaceAll("[^\\p{L}\\s]", "")
这里是。用\p{L}替换正则表达式字符串中的a-zA-Z(任何语言的任何字母)
将字符数组用于所有必要的标点符号是个好主意。如果我错了,请更正我,但for循环结束条件不应该是IreplaceAll("[^\\p{L}\\s]", "")
while (input.hasNext()){
String phrase = input.nextLine();
String[] words = phrase.split(" ");
for(String word: words){
String strippedInput = word.replaceAll("[^0-9\\p{L}\\s]", "");
}
}