我有一个字符串。比方说:
String s = "This is my P.C.. My P.C. is the best.O.M.G!! Check this...";

我想将所有P.C.替换为PC单词,并将O.M.G替换为OMG。通常,我要替换单个字母或单个字母与空格或点之间的所有点。我认为匹配的正则表达式是:

[^A-Za-z][A-Za-z]\\.[A-Za-z\\s\\.][^A-Za-z]


如何仅替换其中的点而不匹配所有匹配项?

编辑:

预期产量:

"This is my PC. My PC is the best.OMG!! Check this..."


编辑2:

基本任务是从可能带有或不带有点的缩写和缩写词中删除点。因此,好的正则表达式也很有价值

最佳答案

您可以考虑使用正向前瞻断言以下是字母,点.或空格。

String s = "This is my P.C.. My P.C. is the best.O.M.G!! Check this...";
String r = s.replaceAll("([A-Z])\\.(?=[ A-Z.])", "$1");
System.out.println(r); //=> "This is my PC. My PC is the best.OMG!! Check this..."

07-28 02:36
查看更多