简体   繁体   English

检查消息是否包含字符串

[英]Checking if a message contains a string

I have have a class that check id a phrase is contained in a message, I tried to do it with Matcher and Pattern and with String.contains() , but the results returned are odd. 我有一个检查id消息中是否包含短语的类,我试图用MatcherPattern以及String.contains()做到这一点,但是返回的结果很奇怪。

Here is the class: 这是课程:

public class MotsClesFilter implements EmailFilter {

    final String NAME = "Filtrage par mots cles";
    /*private Pattern chaineSpam;
    private Matcher chaineCourriel;*/
    private int nbOccMotSpam;
    private byte confidenceLevel;
    @Override
    public String getFilterName() {
        return this.NAME;

    }

    @Override
    public byte checkSpam(MimeMessage message) {
        analyze(message);

        if(this.nbOccMotSpam==0)
            this.confidenceLevel = 1;
        else if (this.nbOccMotSpam>0 && this.nbOccMotSpam<2)
            this.confidenceLevel = CANT_SAY;
        else if (this.nbOccMotSpam>1 && this.nbOccMotSpam<3)
            this.confidenceLevel = 50;
        else if (this.nbOccMotSpam>3 && this.nbOccMotSpam<4)
            this.confidenceLevel = 65;
        else if (this.nbOccMotSpam>4 && this.nbOccMotSpam<5)
            this.confidenceLevel = 85;
        else this.confidenceLevel = 90;
        return (getConfidenceLevel());
    }


    public void analyze(MimeMessage message){
        try {
            List<String> listeChaines = new ArrayList<String>(); 
            BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream(new File("SpamWords.txt"))));
            while(bis.ready()){
                String ligne = bis.readLine();
                listeChaines.add(ligne);
            }

            String mail = ((String.valueOf(message.getContent())));
            //System.out.println(mail);


            for (int j =0; j<listeChaines.size();j++){
                //System.out.println(listeChaines.get(j));
                Pattern chaineSpam = Pattern.compile(listeChaines.get(j),Pattern.CASE_INSENSITIVE);
                Matcher chaineCourriel = chaineSpam.matcher(mail);
                if (chaineCourriel.matches())
                    this.nbOccMotSpam++;

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public byte getConfidenceLevel() {
        // TODO Auto-generated method stub
        return this.confidenceLevel;
    }

    @Override
    public boolean enabled() {
        // TODO Auto-generated method stub
        return true;
    }
}

The results returned by checkSpam are always 1 if use matches and 90 if I use find, it also returns 90 when I use mail.contains(listeChaines.get(j)) . 如果使用match,则checkSpam返回的结果始终为1;如果使用find,则返回90,当我使用mail.contains(listeChaines.get(j))时,它也会返回90。

That means that the message doesn't match any of the strings in the file, but that there are at least 5 strings in the file that can be found inside the message. 这意味着消息与文件中的任何字符串都不匹配,但是文件中至少有5个字符串可以在消息找到。

matches() checks if the whole string matches the pattern. matches()检查整个字符串是否与模式匹配。 Not if some substring matches it. 如果某个子字符串与之匹配则不然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM