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

时间:2022-05-25 20:01:10

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.

我想匹配除* .xhtml之外的所有内容。我有一个servlet听* .xhtml,我想要另一个servlet来捕获其他所有东西。如果我将Faces Servlet映射到所有东西(*),它会在处理图标,样式表和所有非面孔请求时发生爆炸。

This is what I've been trying unsuccessfully.

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

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

Any ideas?

有任何想法吗?

Thanks,

谢谢,

Walter

沃尔特

4 个解决方案

#1


13  

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

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

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

This pattern matches anything that doesn't end with ".xhtml".

此模式匹配任何不以“.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.

#2


7  

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
}       

#3


0  

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).

请注意,如果输入包含扩展名(.something),则上述内容仅匹配。

#4


0  

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.

你真的只是在你的模式结束时错过了一个“$”和一个propper negative look-behind(那个“(^())”没有这样做)。查看语法的特殊构造部分。

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.

正常表达式测试工具在这些情况下非常有用,因为您通常会依赖人们为您仔细检查您的表达式。请不要自己编写,请使用Windows上的RegexBuddy或Mac OS X上的Reggy。这些工具具有允许您选择Java的正则表达式引擎(或类似工具)进行测试的设置。如果您需要测试.NET表达式,请尝试使用Expresso。此外,您可以在他们的教程中使用Sun的测试工具,但这对于形成新表达式并不具有指导意义。

#1


13  

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

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

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

This pattern matches anything that doesn't end with ".xhtml".

此模式匹配任何不以“.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.

#2


7  

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
}       

#3


0  

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).

请注意,如果输入包含扩展名(.something),则上述内容仅匹配。

#4


0  

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.

你真的只是在你的模式结束时错过了一个“$”和一个propper negative look-behind(那个“(^())”没有这样做)。查看语法的特殊构造部分。

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.

正常表达式测试工具在这些情况下非常有用,因为您通常会依赖人们为您仔细检查您的表达式。请不要自己编写,请使用Windows上的RegexBuddy或Mac OS X上的Reggy。这些工具具有允许您选择Java的正则表达式引擎(或类似工具)进行测试的设置。如果您需要测试.NET表达式,请尝试使用Expresso。此外,您可以在他们的教程中使用Sun的测试工具,但这对于形成新表达式并不具有指导意义。