代码之家  ›  专栏  ›  技术社区  ›  Giammarco

Java删除了字符串上的标点(以及所有这些)以保持重音字符

  •  4
  • Giammarco  · 技术社区  · 7 年前

    我需要删除文件中的标点符号,保持重音字符

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

    我无法删除 ’“” 符号和其他

    注: Eclipse filetext.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");
        }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   Wander Nauta    7 年前

    正则表达式 \p{Punct}

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

    如果您想匹配Unicode联盟分类为标点符号的所有内容,请尝试 \p{IsPunctuation} 相反,它总是检查Unicode字符属性,并与示例中的所有点状匹配(以及更多!)。

    要替换空格和标点符号,如您的示例中所示,您可以使用:

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