我正在尝试将字符串拆分为单词,但我想将“a.b.c”保留为单词,并且仅在标点符号单独存在或位于单词末尾时才删除标点符号 例如
"a.b.c" --> "a.b.c"
"a.b." --> "a.b"
例如
String str1 = "abc a.b a. . b, , test"; should return "abc","a.b","a","b","test"
最佳答案
您可以使用:
String str1 = "abc a.b a. . b, , test";
String[] toks = str1.split("\\p{Punct}*\\s+[\\s\\p{Punct}]*");
for (String tok: toks)
System.out.printf(">>> [%s]%n", tok);
>>> [abc]
>>> [a.b]
>>> [a]
>>> [b]
>>> [test]
关于java - 正则表达式 + Java : split a text into words and removing punctuation only if they are alone or at the end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288956/