检查若干个目录中的JSP,检查是否包括类似于
<%@ include file="head.jsp"%>
这样的代码。一句话就是检查一个JSP是否引用了别的文件。因为程序员的写法不一致,上面的写法可能是
<%@ include file="head.jsp"%>
<%@ include file = "head.jsp" %>
<%@ include file="head.jsp" %>
等等,所以想用正则式来检查。请问有谁能帮我下,我是正则式的新手。谢谢!
<%@ include file="head.jsp"%>
这样的代码。一句话就是检查一个JSP是否引用了别的文件。因为程序员的写法不一致,上面的写法可能是
<%@ include file="head.jsp"%>
<%@ include file = "head.jsp" %>
<%@ include file="head.jsp" %>
等等,所以想用正则式来检查。请问有谁能帮我下,我是正则式的新手。谢谢!
[code=Java]
Pattern p = Pattern.compile("<%@\\s*include\\s*file=\"([^\"]*)\"%>");
Matcher m = p.matcher("<%@ include file=\"head.jsp\"%>");
while(m.find()){
System.out.println(m.group(1));
}
code]
Matcher m = p.matcher("<%@ include file=\"head.jsp\"%>");
while(m.find()){
System.out.println(m.group(1));
}
String input="<%@ include file = \"head.jsp\" %>";
if(Pattern.matches(regex, input)) {
System.out.println("Matches!");
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test {
private static Pattern pattern = Pattern.compile("<%\\s*@\\s+include\\s+file\\s*=\\s*\"(.+?)\"\\s*%>");
public static void main(String[] args) {
String[] jsp = {
"<%@ include file=\"head1.jsp\"%>",
"<%@ include file = \"head2.jsp\" %>",
"<%@ include file=\"head3.jsp\" %><%@ include file=\"head4.jsp\" %>"
};
for(int i=0; i < jsp.length; i++) {
Matcher matcher = pattern.matcher(jsp[i]);
System.out.print(i+1);
System.out.print(":");
while(matcher.find()) {
System.out.print("\t");
System.out.print(matcher.group(1));
}
System.out.println();
}
}
}