Walter White
Walter White

Reputation:

Java Regular Expression, match everything but

I would like to match everything but *.xhtml. I have a servlet listening to *.xhtml and I want another servlet to catch everything else. If I map the Faces Servlet to everything (*), it bombs out when handling icons, stylesheets, and everything that is not a faces request.

This is what I've been trying unsuccessfully.

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

Any ideas?

Thanks,

Walter

Upvotes: 6

Views: 20153

Answers (4)

rampion
rampion

Reputation: 89053

What you need is a negative lookbehind (java example).

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

This pattern matches anything that doesn't end with ".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.

Upvotes: 13

dlamblin
dlamblin

Reputation: 45331

You're really only missing a "$" at the end of your pattern and a propper negative look-behind (that "(^())" isn't doing that). Look at the special constructs part of the syntax.

The correct pattern would be:

.*(?<!\.xhtml)$
  ^^^^-------^ This is a negative look-behind group. 

A Regular Expression testing tool is invaluable in these situations when you normally would rely on people to double check your expressions for you. Instead of writing your own please use something like RegexBuddy on Windows or Reggy on Mac OS X. These tools have settings allowing you to choose Java's regular expression engine (or a work-alike) for testing. If you need to test .NET expressions try Expresso. Also, you could just use Sun's test-harness from their tutorials, but it's not as instructive for forming new expressions.

Upvotes: 0

Siddhartha Reddy
Siddhartha Reddy

Reputation: 6211

You could use the negative look-ahead assertion:

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

Please note that the above only matches if the input contains an extension (.something).

Upvotes: 0

Martlark
Martlark

Reputation: 14581

Not regular expresion, but why use that when you don't have to?

String page = "blah.xhtml";

if( page.endsWith( ".xhtml" ))
{
    // is a .xhtml page match
}       

Upvotes: 7

Related Questions