6

私はすべてを一致させたいのですが*.xhtml。リッスンしているサーブレットがあり、*.xhtml他のすべてをキャッチする別のサーブレットが必要です。Faces Servlet をすべてにマップすると ( *)、アイコン、スタイルシート、および Faces リクエスト以外のすべてを処理するときに爆発します。

これは私が失敗してきたことです。

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

何か案は?

ありがとう、

ウォルター

4

4 に答える 4

13

必要なのは、否定的な後読み( Java の例) です。

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.
于 2009-06-30T04:03:22.440 に答える
7

正規表現ではありませんが、必要がないのになぜそれを使用するのでしょうか?

String page = "blah.xhtml";

if( page.endsWith( ".xhtml" ))
{
    // is a .xhtml page match
}       
于 2009-06-30T04:08:35.717 に答える
0

否定先読みアサーションを使用できます。

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

上記は、入力に拡張子 (.something) が含まれている場合にのみ一致することに注意してください。

于 2009-06-30T04:11:45.623 に答える
0

$パターンの最後にある " " と適切な否定後読み (" " はそれを行っていません) だけが欠けてい(^())ます。構文の特別な構造部分を見てください。

正しいパターンは次のようになります。

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

正規表現のテスト ツールは、通常は式の二重チェックを人に頼る必要がある状況で非常に役立ちます。独自に作成する代わりに、Windows の RegexBuddy やMac OS X のReggyなどを使用してください。これらのツールには、テスト用に Java の正規表現エンジン (または同等のエンジン) を選択できる設定があります。.NET 式をテストする必要がある場合は、Expressoを試してください。また、チュートリアルからSun のテスト ハーネスを使用することもできますが、新しい式を作成するための参考にはなりません。

于 2009-06-30T22:35:56.833 に答える