Extract string between two strings in java

后端 未结 4 1246
不思量自难忘°
不思量自难忘° 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:34

    Your regex looks correct, but you're splitting with it instead of matching with it. You want something like this:

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

提交回复
热议问题