我有一个字符串“你叫什么名字?”在如下所示的变量中。
String str="What is your name ?";
String[] splitString =str.split("\\is+");
我想以这样的方式分割字符串,只需要 is 和 ? 之间的那些单词。 IE。使用正则表达式的您的
和名称
谁能告诉我一些解决方案。
最佳答案
你可以使用这样的东西:
String str="What is your name ?";
String[] splitString = str.replaceAll(".*? is (.*) \\?", "$1").split(" ");
// [your, name]
<强> IdeOne demo
更新:如果你想匹配不区分大小写,只需添加不敏感标志:
String str="What is your name ?";
String[] splitString = str.replaceAll("(?i).*? is (.*) \\?", "$1").split(" ");
关于java - java中使用正则表达式分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32972008/