import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<%.*?%>";
final String string = "<%@ page import=\"java.io.*,java.util.*\" %>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title>Auto Refresh</title>\n"
+ " </head>\n"
+ " <body>\n"
+ " <fieldset style=\"width:20%; background-color:#e6ffe6;\">\n"
+ " <legend>Auto refresh</legend>\n"
+ " <h2>Auto Refresh Example</h2>\n"
+ " <%\n"
+ " // Set refresh, autoload time as 1 seconds\n"
+ " response.setIntHeader(\"Refresh\", 1);\n"
+ " // Get current time\n"
+ " Calendar calendar = new GregorianCalendar();\n"
+ " String am_pm;\n"
+ " int hour = calendar.get(Calendar.HOUR);\n"
+ " int minute = calendar.get(Calendar.MINUTE);\n"
+ " int second = calendar.get(Calendar.SECOND);\n"
+ " if(calendar.get(Calendar.AM_PM) == 0)\n"
+ " am_pm = \"AM\";\n"
+ " else\n"
+ " am_pm = \"PM\";\n"
+ " String CT = hour+\":\"+ minute +\":\"+ second +\" \"+ am_pm;\n"
+ " out.println(\"Crrent Time: \" + CT + \"\\n\");\n"
+ " %>\n"
+ " </fieldset>\n"
+ " </body>\n"
+ "</html>";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html