Java正则表达式,但匹配所有内容

前端之家收集整理的这篇文章主要介绍了Java正则表达式,但匹配所有内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想匹配除* .xhtml之外的所有内容.我有一个servlet听* .xhtml,我想要另一个servlet来捕获其他所有东西.如果我将Faces Servlet映射到所有(*),它会在处理图标,样式表和所有非面部请求时发生爆炸.

这是我一直尝试失败的原因.

@H_403_4@Pattern inverseFacesUrlPattern = Pattern.compile(".*(^(\\.xhtml))");

有任何想法吗?

谢谢,

沃尔特

解决方法

你需要的是 negative lookbehind( java example). @H_403_4@String regex = ".*(?<!\\.xhtml)$"; Pattern pattern = Pattern.compile(regex);

此模式匹配任何不以“.xhtml”结尾的内容.

@H_403_4@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."); } } }

所以:

@H_403_4@% javac NegativeLookbehindExample.java && java NegativeLookbehindExample "example.dot" is a match. "example.xhtml" is NOT a match. "example.xhtml.thingy" is a match.

猜你在找的Java相关文章