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

6 java regex seam

我想要匹配一切但是*.xhtml.我有一个servlet监听*.xhtml,我想要另一个servlet来捕获其他所有东西.如果我将Faces Servlet映射到everything(*),它会在处理图标,样式表和所有非面部请求时发生爆炸.

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

Pattern inverseFacesUrlPattern = Pattern.compile(".*(^(\\.xhtml))");
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢,

沃尔特

ram*_*ion 13

你需要的是一个消极的lookbehind(java示例).

String regex = ".*(?<!\\.xhtml)$";
Pattern pattern = Pattern.compile(regex);
Run Code Online (Sandbox Code Playgroud)

此模式匹配任何不以".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.");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以:

% javac NegativeLookbehindExample.java && java NegativeLookbehindExample                                                                                                                                        
"example.dot" is a match.
"example.xhtml" is NOT a match.
"example.xhtml.thingy" is a match.
Run Code Online (Sandbox Code Playgroud)


Mar*_*ark 7

不经常表达,但为什么在你不需要的时候使用它?

String page = "blah.xhtml";

if( page.endsWith( ".xhtml" ))
{
    // is a .xhtml page match
}       
Run Code Online (Sandbox Code Playgroud)