java Java正则表达式,匹配除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1061651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Regular Expression, match everything but
提问by Martlark
I would like to match everything but *.xhtml. I have a servlet listening to *.xhtmland 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
沃尔特
回答by rampion
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".
此模式匹配不以“.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.
回答by Martlark
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
}
回答by Siddhartha Reddy
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) 时才匹配。
回答by dlamblin
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 constructspart 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 RegexBuddyon Windows or Reggyon 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-harnessfrom their tutorials, but it's not as instructive for forming new expressions.
在这些情况下,正则表达式测试工具非常有用,因为您通常会依赖他人为您仔细检查表达式。而不是写你自己请使用类似的使用RegexBuddy在Windows或Reggy在Mac OS X这些工具的设置允许您选择Java的正则表达式引擎(或工作一样)进行测试。如果您需要测试 .NET 表达式,请尝试Expresso。此外,您可以仅使用 Sun 的教程中的测试工具,但它对形成新表达式的指导意义不大。

