我也刚刚才看正则表达式,大家来看看我做得对不对。1.匹配根目录下的default.html或者空.比如/或者/default.html
.*(/|/default\.html)2. 匹配根目录下list目录下*.html.比如:/list/200707.html
.*/list/[1-9][0-9][0-9][0-9][1-2][0-9]\.html3. 匹配根目录下所有*.html文件.比如/20070701.html
.*/[1-9][0-9][0-9][0-9][0-2][0-9][0-3][0-9]\.html我写了个代码试了一下,请各位多多指教public class RegexTest { static String regex1 = ".*(/|/default\\.html)";
static String regex2 = ".*/list/[1-9][0-9][0-9][0-9][1-2][0-9]\\.html";
static String regex3 = ".*/[1-9][0-9][0-9][0-9][0-2][0-9][0-3][0-9]\\.html"; public static void main(String[] args) {
File file = new File("test/html");
printFile(file);
} public static void printFile(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for(File f: files) {
printFile(f);
}
}
boolean match = false;
String fs = file.getAbsolutePath();
fs = fs.replace('\\', '/');
if (fs.matches(regex1)) {
match = true;
} else if (fs.matches(regex2)) {
match = true;
} else if (fs.matches(regex3)) {
match = true;
}
if (match == true) {
System.out.println(fs);
}
return;
}
}
.*(/|/default\.html)2. 匹配根目录下list目录下*.html.比如:/list/200707.html
.*/list/[1-9][0-9][0-9][0-9][1-2][0-9]\.html3. 匹配根目录下所有*.html文件.比如/20070701.html
.*/[1-9][0-9][0-9][0-9][0-2][0-9][0-3][0-9]\.html我写了个代码试了一下,请各位多多指教public class RegexTest { static String regex1 = ".*(/|/default\\.html)";
static String regex2 = ".*/list/[1-9][0-9][0-9][0-9][1-2][0-9]\\.html";
static String regex3 = ".*/[1-9][0-9][0-9][0-9][0-2][0-9][0-3][0-9]\\.html"; public static void main(String[] args) {
File file = new File("test/html");
printFile(file);
} public static void printFile(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for(File f: files) {
printFile(f);
}
}
boolean match = false;
String fs = file.getAbsolutePath();
fs = fs.replace('\\', '/');
if (fs.matches(regex1)) {
match = true;
} else if (fs.matches(regex2)) {
match = true;
} else if (fs.matches(regex3)) {
match = true;
}
if (match == true) {
System.out.println(fs);
}
return;
}
}
String regStr2 = "^\\/list\\/.*\\.html$";
String regStr3 = "^\\/.*\\.html$";
static String regex1 = ".*(/|/default\\.html)";
static String regex2 = ".*/list/\\w*\\.html";
static String regex3 = ".*/\\w*\\.html";\w只表示一个字符,要加上*才能表示多个字符。
String regStr1 = "^\\/(default\\.html)?$";
String regStr2 = "^\\/list\\/.*\\.html$";
String regStr3 = "^\\/.*\\.html$";完全正确
昨天晚上试过了,谢谢!不过还是有一个问题:为什么我把正则放到web.xml文件里,在过滤器init时,读取不行.直接放到程序赋值给String变量里却行呢?百思不得其解!不是一样的么?我print过,一样!怎么匹配这些?
{@top.html}
{@left.html}
{@bottom.html}