Java 正则表达式匹配除三个特定字符串之外的任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27427414/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:22:32  来源:igfitidea点击:

Java Regex-matches anything except three specific string

javaregex

提问by chrisTina

Given such JavaRegexcodes:

鉴于这样的JavaRegex代码:

Pattern pattern = Pattern.compile("[^(bob)(alice)(kitty)]");
String s = "a";
Matcher matcher = pattern.matcher(s);

boolean bl = matcher.find();
System.out.println(bl);

The output is false. Why? The regex [^(bob)(alice)(kitty)]matches any things except bob, aliceor kitty. Then the result should be true, right?

输出是false。为什么?正则表达式[^(bob)(alice)(kitty)]匹配除bob,alice或之外的任何内容kitty。那么结果应该是真的吧?

采纳答案by anubhava

Because your regex is not doing what you think it should be doing.

因为您的正则表达式没有做您认为应该做的事情。

Use this regex with Negative lookahead:

将此正则表达式与Negative lookahead 一起使用

Pattern pattern = Pattern.compile("^(?!bob|alice|kitty).*$");

Your regex: [^(bob)(alice)(kitty)]is using a character classand inside a character class there are no groups.

您的正则表达式:[^(bob)(alice)(kitty)]正在使用字符类,并且在字符类中没有组。

  • (?!bob|alice|kitty)is negative lookahead that means fail the match if any of these 3 words appear at start of input.
  • Important to use anchors ^and $to make sure we're not matching from middle of the string.
  • If you want to avoid matching these 3 words anywhere in inputthen use this regex:

    ^(?!.*?(?:bob|alice|kitty)).*$
    
  • (?!bob|alice|kitty)是负前瞻,这意味着如果这 3 个单词中的任何一个出现在输入的开头,则匹配失败。
  • 重要的是使用锚点^$确保我们不是从字符串的中间匹配。
  • 如果您想避免在输入中的任何位置匹配这 3 个单词,请使用以下正则表达式:

    ^(?!.*?(?:bob|alice|kitty)).*$
    

RegEx Demo

正则表达式演示