我想匹配除* .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.