【问题标题】:How do I manipulate arrays created from a match.find?如何操作从 match.find 创建的数组?
【发布时间】:2014-12-16 05:43:23
【问题描述】:

这是我的代码:

import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.*;


public class EmailReader {


public static void main(String args[]) {
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    try {
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect   ("imap.gmail.com", "email@gmail.com", "password");
        System.out.println(store);

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message messages[] = inbox.getMessages();


        for (Message message : messages) {
            String subject = message.getSubject();
            String location = "AAR|RRC|PSD|TPC|HP|SCC|MCA";
            List<String> list = new ArrayList<String>();
            System.out.println("SUBJECT:  " + subject);
            System.out.println("DATE:  " + message.getSentDate());
            Pattern pattern = Pattern.compile(location);
            Matcher match = pattern.matcher(subject);
            while (match.find()) {
                list.add(match.group());
                System.out.println(list);
            }



        }
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(2);
    }
}
}

我希望程序在搜索保存在位置中的字符串并将它们存储在数组中时读取每封电子邮件。这很好用,但是我希望将位置字符串中的每个缩写都设置为一个用于确定里程的值。我不知道我会怎么做。最后,我希望能够根据找到的数字总结某个日期范围内的电子邮件的所有值。因此,例如,如果 AAR 设置为 15 并出现在 3 个电子邮件主题行中,而 RRC 设置为 8 并出现在 2 个电子邮件主题行中,它将全部汇总并打印出来。

【问题讨论】:

    标签: java arrays regex match


    【解决方案1】:

    最终使用: if(match.group().contains("AAR"){ 然后做这串代码 } 它看起来很草率,因为我有一堆 if 语句但有效。改用开关可能会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多