Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式防止特定关键字的匹配_Java_Regex - Fatal编程技术网

Java正则表达式防止特定关键字的匹配

Java正则表达式防止特定关键字的匹配,java,regex,Java,Regex,我一直在探索消极的向前看和消极的向后看。但他们似乎没有做我想做的事 假设我有一个句子“word1 word2 word3 word4”,我需要一个正则表达式,如果该句子中存在word3或word7,则它可以拒绝匹配。换句话说,matcher.find()应该是false。我该怎么做 理想情况下,我希望捕获的句子尽可能放松。*但是,如果我收紧捕获的句子,我只能阻止匹配,请参见下面的(\\w{4}\\d{1}\\s.*)部分,该部分最后显示为//结果:无任何内容 String content

我一直在探索消极的向前看和消极的向后看。但他们似乎没有做我想做的事

假设我有一个句子“word1 word2 word3 word4”,我需要一个正则表达式,如果该句子中存在word3或word7,则它可以拒绝匹配。换句话说,
matcher.find()
应该是
false
。我该怎么做

理想情况下,我希望捕获的句子尽可能放松。
*
但是,如果我收紧捕获的句子,我只能阻止匹配,请参见下面的
(\\w{4}\\d{1}\\s.*)
部分,该部分最后显示为
//结果:无任何内容

    String contents = null;
    contents = "word1 word2 word3 word4";
    m = Pattern.compile("(?i)(.*)").matcher(contents);
    //Result: word1 word2 word3 word4

    m = Pattern.compile("(?i)(?!.*(word3))(.*)").matcher(contents);
    //Result: ord3 word4

    m = Pattern.compile("(?i)(?!.*(word3))(\\w{4}\\d{1}\\s.*)").matcher(contents);
    //Result: Nothing

    m = Pattern.compile("(?i)(?<!(word3))(.*)").matcher(contents);
    //Result: word1 word2 word3 word4

    m = Pattern.compile("(?i)(.*)(?<!(word3))").matcher(contents);
    //Result: word1 word2 word3 word4

    m = Pattern.compile("(?i)(.*)(?<!(word4))").matcher(contents);
    //Result: word1 word2 word3 word
字符串内容=null;
contents=“word1 word2 word3 word4”;
m=Pattern.compile((?i)(*).matcher(contents);
//结果:word1 word2 word3 word4
m=Pattern.compile((?i)(?!*(word3))(*).matcher(contents);
//结果:ORD3Word4
m=Pattern.compile(“(?i)(?!*(word3))(\\w{4}\\d{1}\\s.*)).matcher(contents);
//结果:什么都没有
m=Pattern.compile(“(?i)(?如何:

Pattern pattern = Pattern.compile("^(?!.*(word1|word2|word3|word4|word5|word6|word7)).*$");
Matcher matcher = patter.matcher(" word1");
System.out.println(matcher.find());
这可以根据这些词中的模式进行简化,例如:

Pattern p = Pattern.compile("^(?!.*(word[1-7])).*$");
Matcher m = p.matcher(" word8");
boolean b = m.find();

我不熟悉消极的傻瓜,但有两条建议:对我来说,Regex看起来不是合适的工具,当你真的需要使用Regex时,我可以推荐这个网站进行调试:先检查句子中是否有单词,如果没有,再应用matcher?谢谢@grzegorz-górkiewicz real我现在承认了我的错误。我错过了$