我正在尝试在字符串中查找单词。然而,由于一段时期的原因,它无法识别一个单词。我试图删除标点符号,但似乎没有效果。我在这里错过了什么吗?这是我正在使用的代码行: s.replaceAll("([a-z] +) [?:!.,;]*","$1");
String test = "This is a line about testing tests. Tests are used to examine stuff";
String key = "tests";
int counter = 0;
String[] testArray = test.toLowerCase().split(" ");
for(String s : testArray)
{
s.replaceAll("([a-z] +) [?:!.,;]*","$1");
System.out.println(s);
if(s.equals(key))
{
System.out.println(key + " FOUND");
counter++;
}
}
System.out.println(key + " has been found " + counter + " times.");
}
我设法通过使用 s = s.replaceAll("\W",""); 找到了一个解决方案(尽管可能并不理想);感谢大家指导如何解决这个问题。
最佳答案
您还可以在拆分操作中利用正则表达式。试试这个:
String[] testArray = test.toLowerCase().split("\\W+");
这将在撇号上分开,因此您可能需要使用特定的字符列表对其进行一些调整。
关于java - 删除标点符号问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15619206/