Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java-substringBetween()函数基于第二次出现提取子字符串_Java_String_Apache Commons_Apache Stringutils_Apache Commons Lang - Fatal编程技术网

使用Java-substringBetween()函数基于第二次出现提取子字符串

使用Java-substringBetween()函数基于第二次出现提取子字符串,java,string,apache-commons,apache-stringutils,apache-commons-lang,Java,String,Apache Commons,Apache Stringutils,Apache Commons Lang,我有以下字符串,如果这是好的,如果这是坏的。要求是从主字符串中提取所需的字符串 使用 substringBetween(mainString, "If", "is") returns the string "this". 在这种情况下,请您帮助提取所需的字符串。如果无法使用函数substringBetween实现这一点,是否有其他字符串函数可以实现这一点?您的意思是StringUtils.substringbetweenfo,If,is代替 StringUtils.SubstringBetwe

我有以下字符串,如果这是好的,如果这是坏的。要求是从主字符串中提取所需的字符串

使用

substringBetween(mainString, "If", "is") returns the string "this".
在这种情况下,请您帮助提取所需的字符串。如果无法使用函数substringBetween实现这一点,是否有其他字符串函数可以实现这一点?

您的意思是StringUtils.substringbetweenfo,If,is代替 StringUtils.SubstringBetweeInfo(如果)是因为方法substringBetween是区分大小写的操作

而在If和is之间搜索的结果与在If和is之间搜索的结果不同

String foo = "If this is good and if that is bad";
String bar = StringUtils.substringBetween(foo, "if", "is");
// String bar = StringUtils.substringBetween(foo, "If", "is");
System.out.println(bar);
您可以使用正则表达式和模式匹配来提取它,例如:

String s = "If this is good and if that is bad";
Pattern pattern = Pattern.compile("if(.*?)is");
Matcher m = pattern.matcher(s);
if(m.find()){
    System.out.println(m.group(1).trim());
}

可能是指令之间的子指令,如果,是坏的??您的预期输出是什么?你得到了什么?这不是我真正的字符串。实际字符串是转换为string的xml。从另一个工具接收时,无法将其转换回xml。存在具有重复信息的子节点。比如,一个旅行段内有多个航班段。因此,问题就来了。如果是xml,你应该使用xml解析器。我的缺点是,我将提供一个更好的主字符串来说明。考虑字符串Foo=,如果这是好的,如果这是好的。我需要通过只给出直接的前导和后继作为substringbetween中的输入来提取它,我尝试了上面的方法,String s=如果这是好的,如果那是坏的;Pattern=Pattern.compileif.*?is;匹配器m=模式匹配器;ifm.find{System.out.printlnm.group1.trim;}我得到的输出不是这个。你知道如何修改它,使它返回一个数组或一个满足正则表达式的所有元素的列表吗?@VidyaShankarNS如果使用ifm.find{尝试使用whilem.find{,它将打印所有匹配项。非常感谢你理解我的问题。这就是我想要的。再次感谢!!