2014-07-22 66 views
1

我想从我的文本数据中只删除标点符号,但保留重音字母。我不想用英文等值替换重音字母。我无法弄清楚如何调整我现有的代码以允许更高的ascii字符。如何只删除标点符号但留下重音字母?

while (input.hasNext()){ 
     String phrase = input.nextLine(); 
     String[] words = phrase.split(" "); 
     for(String word: words){ 
       String strippedInput = word.replaceAll("[^0-9a-zA-Z\\s]", ""); 
     } 
    } 

如果原来的输入是: ØSAL,欧Ø钠代,tambémécontraindicado EM pacientes hipotensos?

预期的输出应该是: ØSAL欧Ø钠代tambémécontraindicado EM pacientes hipotensos

任何想法?谢谢!

回答

2
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(); 
} 
} 
+2

好主意,用一个字符数组所有必要的标点符号。纠正我,如果我错了,但不应该for循环结束条件只是我 AdamMc331

+0

对不起@ McAdam331.Yeah它的真实!它必须是s.length()。想知道我是如何登陆s.length-1的。请更新更正。 –

+0

不需要抱歉。我试图为你编辑,但是Stack需要编辑6个字符。从0开始的索引有时很难遵循。我的意思是,你甚至可以将它改为i <= s.Length() - 1,然后你将有一个有效的for循环。 – AdamMc331

0

这可能是低效的,我敢肯定,这个想法可以改进,但你可以创建一个通过字符串循环,建设一个没有标点的每个字符的缓冲方法。

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); 
     } 
    } 
} 

再次,可能不是最干净或最有效的,但这是我现在可以提出的最好的。

1

也许我错过了点,但像...

String text = "O sal, ou o sódio, também é contraindicado em pacientes hipotensos?"; 
System.out.println(text); 
System.out.println(text.replaceAll("[\\?,.:!\\(\\){}\\[\\]<>%]", "")); 

输出

O sal, ou o sódio, também é contraindicado em pacientes hipotensos? 
O sal ou o sódio também é contraindicado em pacientes hipotensos 

或者,根据你的榜样......

while (input.hasNext()){ 
    String phrase = input.nextLine(); 
    String[] words = phrase.split(" "); 
    for(String word: words){ 
      String strippedInput = word.replaceAll("[\\?,.:!\\(\\){}\\[\\]<>%]", ""); 
    } 
} 
3

考虑使用Unicode Categories,因为“AZ”非常以英语为中心,甚至没有处理发现的口音。

例如,下面将取代一切,包括标点符号,除了 “任何字母,任何语言”(\p{L})或"whitespace"\s)。如果需要保留数字,请将其重新添加为额外的排除项。

replaceAll("[^\\p{L}\\s]", "") 

这里是an ideone demo

2

取代A-ZA-Z在正则表达式的字符串\ p {L}(任何种类的信任何语言)

while (input.hasNext()){ 
    String phrase = input.nextLine(); 
    String[] words = phrase.split(" "); 
    for(String word: words){ 
      String strippedInput = word.replaceAll("[^0-9\\p{L}\\s]", ""); 
    } 
} 
相关问题