我在读文件并从文件中删除所有标点符号时遇到了一些麻烦。 下面是我现在有,我不明白为什么“----”和“*****”仍然会发生。在字符串中删除背对背破折号和星号
任何人都可以在一个方向指出我需要如何调整我的replaceAll()以确保重复出现的标点符号可以被删除吗?
public void analyzeFile(File filepath) {
try {
FileInputStream fStream = new FileInputStream(filepath);
DataInputStream in = new DataInputStream(fStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String textFile = "";
String regex = "[a-zA-Z0-9\\s]";
String putString = "";
wordCount = 0;
while ((textFile = br.readLine()) != null) {
if (!textFile.equals("") && textFile.length() > 0) {
String[] words = textFile.split(" ");
wordCount += words.length;
for (int i = 0; i < words.length; i++) {
putString = cleanString(regex, words[i]);
if(putString.length() > 0){
mapInterface.put(putString, 1);
}
}
putString = "";
}
}
in.close();
} catch (Exception e) {
System.out.println("Error while attempting to read file: "
+ filepath + " " + e.getMessage());
}
}
private String cleanString(String regex, String str){
String newString = "";
Pattern regexChecker = Pattern.compile(regex);
Matcher regexMatcher = regexChecker.matcher(str);
while(regexMatcher.find()){
if(regexMatcher.group().length() != 0){
newString += regexMatcher.group().toString();
}
}
return newString;
}
为了完整起见,你应该问一个问题。 –
请添加一些代码,使其成为最小*工作*示例,声明这些变量,并检查结果。理想情况下,人们只需运行一个最小的公共类Test {public static void main(String [] args){...}}。如果“文本文件”出错,它也会出错,例如,“String input =”hjuk ** * ++ uihe hhued89 vwko **** \ nhus jgirw hifewq“;' –