这是我的案例字符串:
"This is a test. How many??? " + "Senteeeeeeeeeences are here... there should be 5! Right?"使用
String[] words = getText().split("[( +)!.?,]");
我得到这个数组:
[This, is, a, test, , , How, many, , , , , Senteeeeeeeeeences, are, here, , , , there, should, be, 5, , , Right
如何取消空字符串?
答案 0 :(得分:0)
你的问题不够明确。
如果你告诉我,那么不那么 我可以更新答案或删除。
您只需要全局匹配所有非单词。那就是它。
您需要什么
[^\w]+
如何运作
对于拆分,它匹配除[a-zA-Z0-9_]
之外的所有内容。
快速测试
echo "This is a test. How many??? " + "
Senteeeeeeeeeences are here... there should
be 5! Right?" | perl -lne 'print "@{[split( /[^\w]+/g )]}"'
<强>输出强>
This is a test How many Senteeeeeeeeeences are here there should be 5 Right
答案 1 :(得分:0)
String stringToSearch = "This is a test. How many??? " + "Senteeeeeeeeeences are here... there should be 5! Right?";
Pattern p1 = Pattern.compile("[\\W]+");
String[] str = p1.split(stringToSearch);
System.out.println(Arrays.asList(str));
输出:
[这,是,a,测试,如何,很多,Senteeeeeeeeeences,是,在这里,那里,应该,是,5,对]