我正在尝试将一个字符串拆分成单词,但我想保留,“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"
答案 0 :(得分:1)
您可以使用:
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]