java正则表达式在字符串中查找单词在符号中找到空字符串,如。,!(?和空格

时间:2017-02-10 07:13:53

标签: java regex string

这是我的案例字符串:

"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

如何取消空字符串?

2 个答案:

答案 0 :(得分:0)

  

你的问题不够明确。
  如果你告诉我,那么那么   我可以更新答案或删除

您只需要全局匹配所有非单词。那就是它。

您需要什么
[^\w]+

如何运作
对于拆分,它匹配除[a-zA-Z0-9_]之外的所有内容。

prove

快速测试

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,对]