检查邮件是否包含字符串

时间:2013-04-08 21:18:01

标签: java string contains

我有一个类,用于检查错误消息中是否包含短语,我尝试使用MatcherPattern以及String.contains()进行操作,但返回的结果是奇数

这是班级:

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;
    }
}

如果使用匹配,checkSpam返回的结果总是1,如果我使用find,则结果为90,当我使用mail.contains(listeChaines.get(j))时,它也返回90。

1 个答案:

答案 0 :(得分:0)

这意味着该消息与文件中的任何字符串都不匹配,但文件中至少有5个字符串可以在消息中找到

matches()检查整个字符串是否与模式匹配。如果某些子字符串与之匹配则不会。