Java正则表达式,但匹配所有内容
发布时间:2020-12-15 02:56:12 所属栏目:Java 来源:网络整理
导读:我想匹配除* .xhtml之外的所有内容.我有一个servlet听* .xhtml,我想要另一个servlet来捕获其他所有东西.如果我将Faces Servlet映射到所有(*),它会在处理图标,样式表和所有非面部请求时发生爆炸. 这是我一直尝试失败的原因. Pattern inverseFacesUrlPattern =
我想匹配除* .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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |