从java中的字符串中检索subString?

时间:2017-12-08 11:08:54

标签: java regex regex-group

我在java中收到异常消息,我需要异常消息的异常代码。

以下字符串中的示例我只需要提取 535-5.7.8 作为输出

535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials q74sm13073133pfd.134 - gsmtp

下面是代码

  public class mailDemo {
public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host","smtp.gmail.com");
    props.put("mail.smtp.port","587");
    //get Session

    Session session = Session.getDefaultInstance(props);
    Transport transport;
    while (true) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("Enter Email Address ");
        String userName=scanner.nextLine();
        System.out.println("Enter password ");
        String passwd=scanner.nextLine();
        try {
            transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", userName,
                    passwd);
            transport.close();
            System.out.println("SuccessFull");
            //Authentication success
        } catch (Exception e) {
            System.out.println("Session Failed ");
      //  e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

只需使用String.split(" ")[0]即可收到错误代码。