I am trying to create a regular expression to check the occurrence of scriptlet tags in the jsp pages.
Here is the regex that I am using:
\<%(\s+)((.+?)(\n)*(\s+))*%>
The problem I am facing here is that, although there are 3 separate instances to be flagged ideally, but my regex is flagging it all at once.
My requirement is to flag the individual instances only. And not the intermediate codes.
Here is the test code that I am using to test my regex:
<%@ page import="java.io.*,java.util.*" %>
<%
// Get session creation time.
Date createTime = new Date(session.getCreationTime());
// Get last access time of this web page.
Date lastAccessTime = new Date(session.getLastAccessedTime());
String title = "Welcome Back to my website";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");
// Check if this is new comer on your web page.
if (session.isNew()){
title = "Welcome to my website";
session.setAttribute(userIDKey, userID);
session.setAttribute(visitCountKey, visitCount);
}
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
session.setAttribute(visitCountKey, visitCount);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
<body>
<center>
<h1>Session Tracking</h1>
</center>
<table border="1" align="center">
<tr bgcolor="#949494">
<th>Session info</th>
<th>Value</th>
</tr>
<tr>
<td>id</td>
<td><% out.print( session.getId()); %></td>
</tr>
<tr>
<td>Creation Time</td>
<td><% out.print(createTime); %></td>
</tr>
</table>
</body>
</html>
Can anybody help me solve this?
Here is a regex
that matches only the tags:
(<%).*?(%>)
Or you want to return only what is in between:
<%(.*?)%>
Then replace to the first group \1
matched.
If you want the .
to match also new lines, use the (?s)
modifier:
(?s)<%(.*?)%>
Demo here: