简体   繁体   English

在Java中使用正则表达式拆分字符串

[英]split string using regular expression in java

I am having a string "What is your name?" 我有一个字符串“您叫什么名字?” in a variable like as shown below. 如下所示。

String str="What is your name ?";
String[] splitString =str.split("\\is+");

I want to split the string in such a way that I want only those words between is and ? 我想以这样的方式分割字符串,即只希望is之间的那些单词 . ie. 即。 your and name by using regular expression yourname ,使用正则表达式

can anyone tell me some solution for this. 谁能告诉我一些解决方案。

我会做替换和拆分。

 string.replaceFirst(".*\\bis\\b\\s*(.*?)\\s*\\?.*", "$1").split("\\s+");

The poor mans solution would be to extract the substing first and use the split on top of that: 穷人解决方案是先提取减法,然后再使用拆分:

String substring = str.substring(str.indexOf("is")+2,str.indexOf("?"));
String[] splitString =substring.trim().split(" ");

You can use replaceFirst and then split 您可以使用replaceFirst然后split

String str="What is your name?";
String[] splitString =str.replaceFirst(".*[[Ii][Ss]]\\s+","").split("\\s*\\?.*|\\s+");
for (int i=0; i< splitString.length; i++){
    System.out.println("-"+splitString[i]);
}

replaceFirst is needed to delete the first part of string, which is What is . 需要replaceFirst删除字符串的第一部分,即What is The regex .*[[Ii][Ss]]\\\\s+ means - any signs before case insensitive IS and all the spaces after that. 正则表达式.*[[Ii][Ss]]\\\\s+表示-不区分大小写的IS之前的任何符号以及其后的所有空格。 If it'll stay, we will get an additional empty string while splitting. 如果将其保留,我们将在拆分时获得另一个空字符串。

After replacing, it splits the rest string by 替换后,它将剩余的字符串分割为

\\\\s+ one or more whitespaces \\\\s+一个或多个空格

and

\\\\s*\\\\?.* the ? \\\\s*\\\\?.* ? sign with all whitespaces before and any characters after 在所有空格之前和之后签名

You could use something like this: 您可以使用如下形式:

String str="What is your name ?";
String[] splitString = str.replaceAll(".*? is (.*) \\?", "$1").split(" ");
// [your, name]

IdeOne demo IdeOne演示

Update: if you want to match case insensitive, just add the insensitive flag: 更新:如果要匹配不区分大小写的字符,只需添加不区分大小写的标志:

String str="What is your name ?";
String[] splitString = str.replaceAll("(?i).*? is (.*) \\?", "$1").split(" ");

Use the regex is([^\\?]+) and capture the first subgroup and split it This is a slightly longer approach, but is the right way to do this in core Java. 使用regex is([^\\?]+)并捕获第一个子组并将其拆分。这是一个稍长的方法,但是在核心Java中是正确的方法。 You can use a regex library to do this 您可以使用正则表达式库来执行此操作

   import java.util.regex.Matcher;
   import java.util.regex.Pattern;
    //Later
    String pattern="`is([^\?]+)"
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(str);
    var words=m.group(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM