Extract string between two strings in java

后端 未结 4 1247
不思量自难忘°
不思量自难忘° 2020-11-27 05:57

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

String str = \"ZZZZL <%= dsn %> AFFF <%= AFG %>\";
Pattern pattern = Patter         


        
4条回答
  •  旧巷少年郎
    2020-11-27 06:47

    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:

    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));
    }
    

提交回复
热议问题