在java中提取两个字符串之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16597303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:21:36  来源:igfitidea点击:

Extract string between two strings in java

javaregexstring

提问by Tien Nguyen

I try to get string between <%= and %>, here is my implementation:

我尝试在 <%= 和 %> 之间获取字符串,这是我的实现:

String str = "ZZZZL <%= dsn %> AFFF <%= AFG %>";
Pattern pattern = Pattern.compile("<%=(.*?)%>");
String[] result = pattern.split(str);
System.out.println(Arrays.toString(result));

it return

它返回

[ZZZZL ,  AFFF ]

But my expectation is:

但我的期望是:

[ dsn , AFG ]

Where am i wrong and how to correct it ?

我错在哪里以及如何纠正它?

采纳答案by jlordo

Your pattern is fine. But you shouldn't be split()ting it away, you should find()it. Following code gives the output you are looking for:

你的模式很好。但你不应该把split()它丢掉,你应该find()这样做。以下代码提供了您正在寻找的输出:

String str = "ZZZZL <%= dsn %> AFFF <%= AFG %>";
Pattern pattern = Pattern.compile("<%=(.*?)%>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
    System.out.println(matcher.group(1));
}

回答by Henry Keiter

Your regex looks correct, but you're splittingwith it instead of matchingwith it. You want something like this:

您的正则表达式看起来是正确的,但您splitting使用它而不是matching使用它。你想要这样的东西:

// Untested code
Matcher matcher = Pattern.compile("<%=(.*?)%>").matcher(str);
while (matcher.find()) {
    System.out.println(matcher.group());
}

回答by Zon

Jlordo approach covers specific situation. If you try to build an abstract method out of it, you can face a difficulty to check if 'textFrom' is before 'textTo'. Otherwise method can return a match for some other occurance of 'textFrom' in text.

Jlordo 方法涵盖特定情况。如果您尝试从中构建抽象方法,您可能会面临检查“ textFrom”是否在“ textTo”之前的困难。否则,方法可以返回textFrom文本中其他出现的“ ”的匹配项。

Here is a ready-to-go abstract method that covers this disadvantage:

这是一个现成的抽象方法,它涵盖了这个缺点:

  /**
   * Get text between two strings. Passed limiting strings are not 
   * included into result.
   *
   * @param text     Text to search in.
   * @param textFrom Text to start cutting from (exclusive).
   * @param textTo   Text to stop cuutting at (exclusive).
   */
  public static String getBetweenStrings(
    String text,
    String textFrom,
    String textTo) {

    String result = "";

    // Cut the beginning of the text to not occasionally meet a      
    // 'textTo' value in it:
    result =
      text.substring(
        text.indexOf(textFrom) + textFrom.length(),
        text.length());

    // Cut the excessive ending of the text:
    result =
      result.substring(
        0,
        result.indexOf(textTo));

    return result;
  }

回答by Pini Cheyni

I have answered this question here: https://stackoverflow.com/a/38238785/1773972

我在这里回答了这个问题:https: //stackoverflow.com/a/38238785/1773972

Basically use

基本使用

StringUtils.substringBetween(str, "<%=", "%>");

This requirs using "Apache commons lang" library: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4

这需要使用“Apache commons lang”库:https: //mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.4

This library has a lot of useful methods for working with string, you will really benefit from exploring this library in other areas of your java code !!!

这个库有很多有用的处理字符串的方法,你会真正受益于在你的 Java 代码的其他领域探索这个库!!!