Java regular expression, but matches everything

I want to match division * Everything except XHTML I have a servlet to listen to * XHTML, I want another servlet to capture everything else If I map faces servlet to all (*), it will explode when processing icons, style sheets, and all non face requests

That's why I've been trying to fail

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

Any ideas?

thank you,

Walter

Solution

What you need is negative look behind (java example)

String regex = ".*(?<!\\.xhtml)$";
Pattern pattern = Pattern.compile(regex);

This pattern matches anything that does not end in ". 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.");
    }
  }
}

So:

% javac NegativeLookbehindExample.java && java NegativeLookbehindExample                                                                                                                                        
"example.dot" is a match.
"example.xhtml" is NOT a match.
"example.xhtml.thingy" is a match.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>