我想在Java中删除文本的标点符号。我知道有一种模式可以匹配所有标点符号,即\p{Punct},但这将删除所有标点符号。但是,我希望保留首字母缩写和连字符单词。例如,保持"m.i.t.“或者“最新技术”,"9.4","11:00","p.m.","976-4275“,而我正在删除标点符号。
我尝试了\p{Punct},但它将删除所有标点符号。
String text = "There's a string from M.I.T., written by Jason at 11:00 p.m. 976-4275, 9.5, another word is state-of-the-art.";
text.replaceAll("\\p{Punct}", "");
System.out.println(text);结果将是:
"There s a string from MIT written by Jason at 1100 pm 9764275 95 another word is stateoftheart"但我想要的是:
"There s a string from M.I.T. written by Jason at 11:00 p.m. 976-4275 9.5 another word is state-of-the-art"发布于 2019-08-09 11:42:53
请在\\p{Punct}后面添加代码&&[^.],它将帮助您替换除句号标点符号以外的所有标点符号。
解决方案:
text.replaceAll("[\\p{Punct}&&[^.]]", "");https://stackoverflow.com/questions/57423113
复制相似问题