Java 匹配器可以';不匹配

Java 匹配器可以';不匹配,java,regex,matcher,Java,Regex,Matcher,我有以下代码。我需要检查文本中存在的任何单词从一些禁止的单词列表。但是即使这个词存在于文本匹配器中,也看不到它。代码如下: final ArrayList<String> regexps = config.getProperty(property); for (String regexp: regexps){ Pattern pt = Pattern.compile("(" + regexp + ")", Pattern.CASE_INSENSITIVE); Mat

我有以下代码。我需要检查文本中存在的任何单词从一些禁止的单词列表。但是即使这个词存在于文本匹配器中,也看不到它。代码如下:

final ArrayList<String> regexps = config.getProperty(property);
   for (String regexp: regexps){
   Pattern pt = Pattern.compile("(" + regexp + ")", Pattern.CASE_INSENSITIVE);
   Matcher mt = pt.matcher(plainText);                        
   if (mt.find()){
      result = result + "message can't be processed because it doesn't satisfy the rule " + property;
      reason = false;
      System.out.println("reason" + mt.group() + regexp);
                        }
                    }
final ArrayList regexps=config.getProperty(property);
for(字符串regexp:regexps){
Pattern pt=Pattern.compile(“+regexp+”),Pattern.CASE不区分大小写);
Matcher mt=pt.Matcher(纯文本);
if(mt.find()){
结果=结果+“无法处理消息,因为它不满足规则”+属性;
原因=错误;
System.out.println(“原因”+mt.group()+regexp);
}
}
怎么了?该代码无法在
明文中找到regexp


//代码>。我还尝试了另一种
regexp
的变体,但一切都是无用的

麻烦在别处

import java.util.regex.*;

public class HelloWorld {

    public static void main(String []args) {
        Pattern pt = Pattern.compile("(qwer)");
        Matcher mt = pt.matcher("asdf qwer zxcv");
        System.out.println(mt.find());
    }
}
这是真的。您可能希望使用单词边界作为分隔符,但:

import java.util.regex.*;

public class HelloWorld {

    public static void main(String []args) {
        Pattern pt = Pattern.compile("\\bqwer\\b");
        Matcher mt = pt.matcher("asdf qwer zxcv");
        System.out.println(mt.find());
        mt = pt.matcher("asdfqwer zxcv");
        System.out.println(mt.find());
    }
}

除非需要在组中捕获关键字,否则括号是无用的。但是您已经有了它。

使用ArrayList的内置函数
indexOf(Object o)
contains(Object o)
检查数组中的任何位置是否存在字符串。 e、 g

ArrayList关键字=新建ArrayList();
关键词.add(“你好”);
System.out.println(关键字.contains(“hello”));
System.out.println(关键字.indexOf(“hello”));
输出:
正确

0

尝试使用下面使用
运算符的正则表达式筛选出包含禁用词的消息

private static void findBannedWords() {
    final ArrayList<String> keywords = new ArrayList<String>();
    keywords.add("f$%k");
    keywords.add("s!@t");
    keywords.add("a$s");

    String input = "what the f$%k";

    String bannedRegex = "";
    for (String keyword: keywords){
        bannedRegex =  bannedRegex + ".*" + keyword + ".*" + "|";
    }

    Pattern pt = Pattern.compile(bannedRegex.substring(0, bannedRegex.length()-1));
    Matcher mt = pt.matcher(input);
    if (mt.matches()) {
         System.out.println("message can't be processed because it doesn't satisfy the rule ");
    }
}
private static void findBannedWords(){
最终ArrayList关键字=新ArrayList();
关键词.添加(“f$%k”);
关键词.add(“s!@t”);
关键词:添加(“a$s”);
String input=“什么是f$%k”;
字符串bannedRegex=“”;
for(字符串关键字:关键字){
banndregex=banndregex+“*”+关键字+“*”+“|”;
}
Pattern pt=Pattern.compile(banndregex.substring(0,banndregex.length()-1));
匹配器mt=pt.匹配器(输入);
如果(mt.matches()){
System.out.println(“无法处理消息,因为它不符合规则”);
}
}

括号在“(“+关键字+”)中的作用是什么?为什么不使用
纯文本。包含(关键字)
?能否提供
关键字集和
纯文本字符串的值?@nikitabloglazov它应该是
“*”+关键字+”
,无论如何,它不适用于多行输入文本。Nikita,我不仅需要检查单词,还需要检查正则表达式,顺便说一句,它甚至不适用于简单的单词。如何遍历输入文本中的每个单词?那么区分大小写呢?如果输入文本是指你的黑名单单词,请像当前一样仔细检查它们,然后使用
关键字。contains(关键字)
,我不认为区分大小写是一个问题-确保你的关键字大小写正确,或者确保所有内容都
toLower()
。我还是不明白你想说什么。我改变了这一点:Pattern pt=Pattern.compile(“(qwer)”);对此:Pattern pt=Pattern.compile(“\\bqwer\\b”);但仍然有相同的结果。它在文本中找不到任何单词我不明白它如何改变结果。为什么在这之后它开始寻找合适的词语?@NikitinMikhail它不会改变结果。它将帮助我们诊断问题。我怀疑明文或关键字都有问题。顺便说一句,也试着打印出关键词:)你不明白。我知道这是用来诊断问题的。我在开玩笑,因为贝根的方法是正确的。我所做的唯一一件事就是这样更改了一个字符串:Pattern pt=Pattern.compile(“(+keyword+”),Pattern.CASE\u不区分大小写);我试图听从您的建议,但收到以下异常“^java.util.regex.PatternSyntaxException:\k后面没有”
private static void findBannedWords() {
    final ArrayList<String> keywords = new ArrayList<String>();
    keywords.add("f$%k");
    keywords.add("s!@t");
    keywords.add("a$s");

    String input = "what the f$%k";

    String bannedRegex = "";
    for (String keyword: keywords){
        bannedRegex =  bannedRegex + ".*" + keyword + ".*" + "|";
    }

    Pattern pt = Pattern.compile(bannedRegex.substring(0, bannedRegex.length()-1));
    Matcher mt = pt.matcher(input);
    if (mt.matches()) {
         System.out.println("message can't be processed because it doesn't satisfy the rule ");
    }
}