qw-6ren">
gpt4 book ai didi

Java 删除字符串上的标点符号(还有 ’ “” 和所有这些)保持重音字符

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:15 25 4
gpt4 key购买 nike

我需要删除文件中的标点符号,保持重音字符我试过这段代码,但无法正常工作。

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

最佳答案

默认情况下,正则表达式 \p{Punct} 仅匹配 US-ASCII 标点符号,除非您启用 Unicode 字符类。这意味着您编写的代码只会删除这些字符:

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

如果您想匹配 Unicode 联盟归类为标点符号的所有内容,请尝试使用 \p{IsPunctuation},它始终检查 Unicode 字符属性并匹配示例中的所有标点符号(以及更多!) .

要替换空格和标点符号,就像在您的示例中一样,您可以使用:


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

关于Java 删除字符串上的标点符号(还有 ’ “” 和所有这些)保持重音字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com