I have answered this question here: 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
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 !!!