java正则表达式在字符串中查找单词在符号中查找空字符串,如。,!(?和空的空间
这是我的案例字符串: "This is a test. How many??? " + "Senteeeeeeeeeences are here... there should be 5! Right?" “这是一个测试。有多少?”+“这里有四个……应该有五个!对吗?” 使用java正则表达式在字符串中查找单词在符号中查找空字符串,如。,!(?和空的空间,java,regex,string,Java,Regex,String,这是我的案例字符串: "This is a test. How many??? " + "Senteeeeeeeeeences are here... there should be 5! Right?" “这是一个测试。有多少?”+“这里有四个……应该有五个!对吗?” 使用 String[]words=getText().split(“[(+)!.?,]”; 我得到这个数组: [这是一个测试,有多少个,有多少个,应该是,5个,对 String stringToSearch = "This
String[]words=getText().split(“[(+)!.?,]”;
我得到这个数组:
[这是一个测试,有多少个,有多少个,应该是,5个,对
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));
如何对空字符串进行eleiminate
你的问题不够清楚。如果它不是你去告诉我的那样,那么 我可以更新答案或删除答案
您只需全局匹配所有非单词即可 你需要什么
[^\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 )]}"'
输出这是一个测试,这里有多少个字符,应该有5个正确的
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));
输出:
[This,is,a,test,How,many,senteeces,are,here,should,be,be,5,Right]使用什么标准进行拆分?仅使用这些符号或任何空格/标点符号?可能需要
拆分([\\p{punch}\\s]+)
?@MiniBug您只想提取单词吗?不需要其他任何东西。谢谢@WiktorStribiżew。您的解决方案字符串[]words=getText().split([\\s+()!?,]+”);
非常适合。我可以发布吗?我看到您接受了另一个解决方案。