我想匹配除 *.xhtml
之外的所有内容。我有一个 servlet 正在监听 *.xhtml
并且我想要另一个 servlet 来捕获其他所有内容。如果我将 Faces Servlet 映射到所有内容 (*
),它会在处理图标、样式表和所有非面部请求的内容时崩溃。
这是我一直在尝试但未成功的。
Pattern inverseFacesUrlPattern = Pattern.compile(".*(^(\\.xhtml))");
有什么想法吗?
谢谢,
沃尔特
最佳答案
您需要的是 negative lookbehind (java example)。
String regex = ".*(?<!\\.xhtml)$";
Pattern pattern = Pattern.compile(regex);
此模式匹配任何不以“.xhtml”结尾的内容。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NegativeLookbehindExample {
public static void main(String args[]) throws Exception {
String regex = ".*(?<!\\.xhtml)$";
Pattern pattern = Pattern.compile(regex);
String[] examples = {
"example.dot",
"example.xhtml",
"example.xhtml.thingy"
};
for (String ex : examples) {
Matcher matcher = pattern.matcher(ex);
System.out.println("\""+ ex + "\" is " + (matcher.find() ? "" : "NOT ") + "a match.");
}
}
}
所以:
% javac NegativeLookbehindExample.java && java NegativeLookbehindExample
"example.dot" is a match.
"example.xhtml" is NOT a match.
"example.xhtml.thingy" is a match.
关于Java正则表达式,匹配除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1061651/