捕捉 scriptlet 标签的正则表达式

Regular Expression to catch a scriplet tag

我正在尝试创建一个正则表达式来检查 jsp 页中 scriptlet 标记的出现。

这是我正在使用的正则表达式:

\<%(\s+)((.+?)(\n)*(\s+))*%>

我在这里面临的问题是,虽然理想情况下有 3 个单独的实例需要标记,但我的正则表达式会同时标记所有实例。

我的要求是只标记个别实例。而不是中间代码。

这是我用来测试正则表达式的测试代码:

<%@ 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>

谁能帮我解决这个问题?

这里是 regex 只匹配标签:

(<%).*?(%>)

或者您只想 return 介于两者之间的内容:

<%(.*?)%>

然后替换到第一组</code>匹配。</p> <p>如果您希望 <code>. 也匹配新行,请使用 (?s) 修饰符:

(?s)<%(.*?)%>

此处演示:

https://regex101.com/r/jX9bX0/8