4

我需要删除标点读取文件,保持口音字符 我试过这个代码,但不工作我会怎么做。Java删除标点符号字符串(也是“”“和所有这些)保持重音字符

Expectation: input=> ’'qwe..,rty ‘èeéò’“ ”o" "à  output=> qwertyèeéòoà 

Effective result: input=> ’'qwe..,rty ‘èeéò’“ ”o" "à output=>’qwerty ‘èeéò’“ ”o" "à 

我不能删除’“”符号等这些

注:Eclipsefiletext.txt设置为UTF-8

谢谢

import java.io.*; 
import java.util.Scanner; 

public class DataCounterMain { 
    public static void main (String[] args) throws FileNotFoundException { 

    File file = new File("filetext.txt"); 

    try { 
     Scanner filescanner = new Scanner(file); 
     while (filescanner.hasNextLine()) { 

      String line = filescanner.nextLine(); 
      line=line.replaceAll ("\\p{Punct}", ""); 

      System.out.println(line); 
     } 
    } 
    catch(FileNotFoundException e) { 
     System.err.println(file +" FileNotFound"); 
    } 
    } 
} 
+0

看来要删除所有Unicode标点和符号。使用'line = line.replaceAll(“(?U)[\\ p {S} \\ p {P}] +”,“”);' –

回答

5

正则表达式\p{Punct}默认情况下只匹配US-ASCII标点符号,除非您启用Unicode字符类。这意味着你的代码,因为写的,只会删除这些字符:如果你想匹配所有Unicode协会列为标点符号,尝试\p{IsPunctuation}相反,它总是检查Unicode字符属性和所有punctiuation匹配

!"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~ 

在你的例子中(和更多!)。

要替换的空白以及标点符号,就像在你的榜样,你可以使用:

    
     line = line.replaceAll("\\p{IsPunctuation}|\\p{IsWhite_Space}", ""); 
        
+0

它的工作原理是我想要的。谢谢!! – Giammarco

+0

徘徊!你能否将修改后的DataCounterMain类放在你的答案中以便更清晰。 –

+1

@VinayPrajapati我已经添加了一个例子。 –

相关问题