我正在尝试创建一个正则表达式来检查jsp页面中scriptlet标记的出现情况。
下面是我使用的正则表达式:
\<%(\s+)((.+?)(\n)*(\s+))*%>我在这里面临的问题是,虽然有3个单独的实例在理想情况下被标记,但是我的regex正在同时标记它。
我的要求是只标记单个实例。而不是中间代码。
下面是用于测试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>有人能帮我解决这个问题吗?
发布于 2016-05-17 17:11:41
下面是一个只匹配标记的regex:
(<%).*?(%>)或者您只想返回介于两者之间的内容:
<%(.*?)%>然后替换为第一个组\1匹配。
如果希望.也匹配新行,请使用(?s)修饰符:
(?s)<%(.*?)%>这里的演示:
https://stackoverflow.com/questions/37269494
复制相似问题